Month: March 2017

  • Golang: Get the function caller’s name

    Problem Consider this code: package main import “fmt” func foo() { // foo() wants to know who called it fmt.Println(“HI”) } func main() { foo() } In the function foo, we want to get the name of the function (and preferably file name and number ) that called it. Solution We can get this information…

  • Golang: Connect to Postgres and selecting data from a table

    You will need to get the Postgres driver first. In the terminal type: go get github.com/lib/pq Connecting to Postgres package main import ( _ “github.com/lib/pq” “database/sql” “fmt” ) func main() { // Connect to the DB, panic if failed db, err := sql.Open(“postgres”, “postgres://user:password@localhost/dbName?sslmode=disable”) if err != nil { fmt.Println(`Could not connect to db`) panic(err)…