Do we need to close the response object if an error occurs while calling http.Get(url)?

General concept is that when a function (or method) has multi return values one being an error, error should be checked first and only proceed if the error is nil. Functions should return zero values for other (non-error) values if there is an error. If the function behaves differently, it should be documented. http.Get() does … Read more

Registering packages in Go without cyclic dependency

The standard library solves this problem in multiple ways: 1) Without a “Central” Registry Example of this is the different hash algorithms. The crypto package just defines the Hash interface (the type and its methods). Concrete implementations are in different packages (actually subfolders but doesn’t need to be) for example crypto/md5 and crypto/sha256. When you … Read more

Syntax error at end of input in PostgreSQL

You haven’t provided any details about the language/environment, but I’ll try a wild guess anyway: MySQL’s prepared statements natively use ? as the parameter placeholder, but PostgreSQL uses $1, $2 etc. Try replacing the ? with $1 and see if it works: WHERE address = $1 The error messages in PostgreSQL are very cryptic. In … Read more

Golang parse a json with DYNAMIC key [duplicate]

I believe you want something like this: type Person struct { Name string `json:”name”` Age int `json:”age”` } type Info map[string]Person Then, after decoding this works: fmt.Printf(“%s: %d\n”, info[“bvu62fu6dq”].Name, info[“bvu62fu6dq”].Age) Full example: http://play.golang.org/p/FyH-cDp3Na

What does a function without body mean?

The function is defined here: // startTimer adds t to the timer heap. //go:linkname startTimer time.startTimer func startTimer(t *timer) { if raceenabled { racerelease(unsafe.Pointer(t)) } addtimer(t) } Function declarations: A function declaration may omit the body. Such a declaration provides the signature for a function implemented outside Go, such as an assembly routine. Not every … Read more

How to format timestamp in outgoing JSON

What you can do is, wrap time.Time as your own custom type, and make it implement the Marshaler interface: type Marshaler interface { MarshalJSON() ([]byte, error) } So what you’d do is something like: type JSONTime time.Time func (t JSONTime)MarshalJSON() ([]byte, error) { //do your serializing here stamp := fmt.Sprintf(“\”%s\””, time.Time(t).Format(“Mon Jan _2”)) return []byte(stamp), … Read more

How to get the name of a function in Go?

I found a solution: package main import ( “fmt” “reflect” “runtime” ) func foo() { } func GetFunctionName(i interface{}) string { return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() } func main() { // This will print “name: main.foo” fmt.Println(“name:”, GetFunctionName(foo)) }