Gorilla mux optional query values

I would just register your handler twice. router.Path(“/articles/{id:[0-9]+}”). Queries(“key”, “{[0-9]*?}”). HandlerFunc(YourHandler). Name(“YourHandler”) router.Path(“/articles/{id:[0-9]+}”).HandlerFunc(YourHandler) Here is a working program to demonstrate. Notice that I am using r.FormValue to get the query parameter. Note: make sure you have an up to date version go get -u github.com/gorilla/mux since a bug of query params not getting added the … Read more

How to implement generic interfaces?

Types don’t actually implement generic interfaces, they implement instantiations of generic interfaces. You can’t use a generic type (including interfaces) without instantiation. From there, it is just like pre-generics Go, including the difference between methods with pointer receiver. Therefore it is helpful to think what the methods that use type parameters would look like if … Read more

Decoding JSON int into string

You can use the type json.Number which is implemented as a string: type User struct { Id json.Number `json:”user_id”` Username string `json:”user_name”` } Then you can simply convert it in any other code: stringNumber := string(userInstance.Id) Playground: https://play.golang.org/p/2BTtWKkt8ai

Go gin get request body json

If you only want to pass the request body JSON to Redis as a value, then you do not need to bind the JSON to a value. Read the raw JSON from the request body directly and just pass it through: jsonData, err := ioutil.ReadAll(c.Request.Body) if err != nil { // Handle error } err … Read more

How should I use vendor in Go 1.6?

With Go1.6, vendoring is built in as you read. What does this mean? Only one thing to keep in mind: When using the go tools such as go build or go run, they first check to see if the dependencies are located in ./vendor/. If so, use it. If not, revert to the $GOPATH/src/ directory. … Read more

Append not thread-safe?

In Go no value is safe for concurrent read/write, slices (which are slice headers) are no exception. Yes, your code has data races. Run with the -race option to verify. Example: type myClass struct { AttributeName string } sourceSlice := make([]myClass, 100) destSlice := make([]myClass, 0) var wg sync.WaitGroup for _, myObject := range sourceSlice … Read more

Golang Converting image.Image to []byte

You want a bytes.Buffer, not a bufio.Writer. bytes.Buffer is used when you need a writer that writes to memory. bufio.Writer just caches data in memory before forwarding it to another writer. buf := new(bytes.Buffer) err := jpeg.Encode(buf, new_image, nil) send_s3 := buf.Bytes()

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