Created
March 23, 2015 07:47
-
-
Save smahs/5637eef8fe4d6593b26f to your computer and use it in GitHub Desktop.
Emulating try-catch in #golang with multi-return statements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hids [][]string | |
db, err := sql.Open("postgres", "user=myuser dbname=mydb sslmode=disable") | |
if err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} else { | |
defer db.Close() | |
rows, err := db.Query(my_query) | |
if err != nil { | |
log.Println(err) | |
os.Exit(0) // Because there is no data to fetch | |
} else { | |
// rows.Next() handles rows.Close(), no need for deferring | |
var _hid, _myid string | |
for rows.Next() { | |
if err := rows.Scan(&_hid, &_myid); err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
hids = append(hids, []string{_hid, _myid}) | |
} | |
fmt.Println(hids) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment