How do I send a JSON string in a POST request in Go

I’m not familiar with napping, but using Golang’s net/http package works fine (playground): func main() { url := “http://restapi3.apiary.io/notes” fmt.Println(“URL:>”, url) var jsonStr = []byte(`{“title”:”Buy cheese and bread for breakfast.”}`) req, err := http.NewRequest(“POST”, url, bytes.NewBuffer(jsonStr)) req.Header.Set(“X-Custom-Header”, “myvalue”) req.Header.Set(“Content-Type”, “application/json”) client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer … Read more

Removing packages installed with go get

It’s safe to just delete the source directory and compiled package file. Find the source directory under $GOPATH/src and the package file under $GOPATH/pkg/<architecture>, for example: $GOPATH/pkg/windows_amd64.

Correct way to initialize empty slice

The two alternative you gave are semantically identical, but using make([]int, 0) will result in an internal call to runtime.makeslice (Go 1.16). You also have the option to leave it with a nil value: var myslice []int As written in the Golang.org blog: a nil slice is functionally equivalent to a zero-length slice, even though … Read more

Contains method for a slice

Mostafa has already pointed out that such a method is trivial to write, and mkb gave you a hint to use the binary search from the sort package. But if you are going to do a lot of such contains checks, you might also consider using a map instead. It’s trivial to check if a … Read more

Iterating over all the keys of a map

https://play.golang.org/p/JGZ7mN0-U- for k, v := range m { fmt.Printf(“key[%s] value[%s]\n”, k, v) } or for k := range m { fmt.Printf(“key[%s] value[%s]\n”, k, m[k]) } Go language specs for for statements specifies that the first value is the key, the second variable is the value, but doesn’t have to be present.

Checking the equality of two slices

You should use reflect.DeepEqual() DeepEqual is a recursive relaxation of Go’s == operator. DeepEqual reports whether x and y are “deeply equal,” defined as follows. Two values of identical type are deeply equal if one of the following cases applies. Values of distinct types are never deeply equal. Array values are deeply equal when their … Read more

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