[Solved] How to convert the dynamically produced array object data into JSON format string in golang?

This code will works package main import ( “bytes” “encoding/json” “fmt” “log” “strings” ) type Item struct { Id int `json:”id”` Category string `json:”category”` Name string `json:”name”` Description string `json:”description”` } type Items []Item var myJson = []byte(`[{ “id”:1, “category”:”fruits”, “name”:”Apple”, “description”:”Apple is my favorite fruit.” }, { “id”:2, “category”:”colors”, “name”:”Red”, “description”:”Red color is always … Read more

[Solved] Need understanding of goroutines [duplicate]

Program execution: When the function main returns, the program exits. It does not wait for other (non-main) goroutines to complete. 1- main is goroutine too, you need to wait for other goroutines to finish, and you may use time.Sleep(5 * time.Second) for 5 Seconds wait, try it on The Go Playground: package main import ( … Read more

[Solved] Can’t serve external CSS File in Go webapp

You have to set the Content-Type header in the response correctly. Without the content type, most browsers will not execute the css. Something like the following should work, but this is essentially just a sketch: http.HandleFunc(“/static/”,func(wr http.ResponseWriter,req *http.Request) { // Determine mime type based on the URL if req.URL.Path.HasSuffix(“.css”) { wr.Header().Set(“Content-Type”,”text/css”) } http.StripPrefix(“/static/”, fs)).ServeHTTP(wr,req) }) … Read more

[Solved] Why is this function not thread safe in golang?

I haven’t analyzed all of it, but definitely the modification of mapStore from multiple goroutines is unsafe: mapStore[*res.someData] = append(mapStore[*res.someData], res) But as a starting point, run this under the race detector. It’ll find many problems for you. This is also clearly unsafe: resSlice := append(resSlice, res) But it also doesn’t quite do what you … Read more

[Solved] How to decode embedded json? [closed]

There are several issues in your code. First of all, all the fields of your edit struct must be exported which means you should capitalize the first letter of every fields. Just like the following: type edit struct { Id Values } The data type of of key of the Values field must be string … Read more

[Solved] Multiple line plots sharing abscissas axis in gonum/plot

Yes, it is possible. You can use plot.Align: package main import ( “math/rand” “os” “gonum.org/v1/plot” “gonum.org/v1/plot/plotter” “gonum.org/v1/plot/vg” “gonum.org/v1/plot/vg/draw” “gonum.org/v1/plot/vg/vgimg” ) func main() { rand.Seed(int64(0)) const rows, cols = 2, 1 plots := make([][]*plot.Plot, rows) for j := 0; j < rows; j++ { plots[j] = make([]*plot.Plot, cols) for i := 0; i < cols; i++ … Read more

[Solved] How to import the package in the Go

You must provide only 1 package within single folder. Consider to name package like the folder. In your case create folder “utils” and move your helper.go in there. Please, don’t forget to name public types, vars and funcs properly: start their names with uppercase symbol: Finally your project would look like this: Your helper.go would … Read more

[Solved] Copy array of strings to array of string pointers

To understand what is happening under the hood you must understand the pointer and value semantics of for range construct in go. It is clearly explained in this ardan labs article emails := []string{“a”, “b”} CCEmails := []*string{} for _, cc := range emails { p := &cc fmt.Println(cc, p) CCEmails = append(CCEmails,&cc) } The … Read more

[Solved] Goroutine loop not completing

Finally figured the answer… The problem was that I needed to close my monitoringChan in the first goroutine and then monitor (Defer wg.close()) in the second goroutine. Worked great when I did that! https://play.golang.org/p/fEaZXiWCLt- solved Goroutine loop not completing

[Solved] Time precision issue on comparison in mongodb driver in Go and possibly in other language and other database

Times in BSON are represented as UTC milliseconds since the Unix epoch (spec). Time values in Go have nanosecond precision. To round trip time.Time values through BSON marshalling, use times truncated to milliseconds since the Unix epoch: func truncate(t time.Time) time.Time { return time.Unix(0, t.UnixNano()/1e6*1e6) } … u := user{ Username: “test_bson_username”, Password: “1234”, UserAccessibility: … Read more