Golang io/ioutil NopCloser

Whenever you need to return an io.ReadCloser, while making sure a Close() is available, you can use a NopCloser to build such a ReaderCloser. You can see one example in this fork of gorest, in util.go //Marshals the data in interface i into a byte slice, using the Marhaller/Unmarshaller specified in mime. //The Marhaller/Unmarshaller must … 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

Golang struct initialization

The first method e1 := Event{Id: 1, Name: “event 1”} is initializing the variable e1 as a value with type Event. The second e2 := &Event{Id: 1, Name: “event1”} is initializing e2 as a pointer to a value of type Event As you stated in the comments, the set of methods defined on a value … Read more

What’s the meaning of the new tilde token ~ in Go?

In Go generics, the ~ tilde token is used in the form ~T to denote the set of types whose underlying type is T. It was also called “approximation” constraint element in the generics proposal, which explains what it’s good for in plain language: Listing a single type is useless by itself. For constraint satisfaction, … Read more

How to redirect to a url

The http status 303 is the appropriate response here. So redirect the request with it. if r.Method == “POST” { saveChoice(r.Form[“choices”]) http.Redirect(w, r, newUrl, http.StatusSeeOther) } And if your newUrl is supposed to return a proper html page to the browser, you don’t need to use ajax. Use an html form. <form action=”/postHandler” method=”post”> {{range … Read more

What is the Go equivalent to assert() in C++?

As mentioned by commenters, Go does not have assertions. A comparable alternative in Go is the built-in function panic(…), gated by a condition: if condition { panic(err) } This article titled “Defer, Panic, and Recover” may also be informative.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)