Passing pointer of string to a function leads to the wrong string being printed

I have the following program (simplified as much as possible): package main import "fmt" type Test struct { Str *string } func main() { arr := []string{"vanC", "vanD"} arr2 := make([]Test, 0) for _, element := range arr { fmt.Println(element, &element) arr2 = append(arr2, Test{ Str: &element, }) } fn(arr2) } func fn(arr2 []Test) {… Read More Passing pointer of string to a function leads to the wrong string being printed

Go return struct as JSON in HTTP request

I’ve defined the following struct in Go: type repoStars struct { name string owner string stars int } And I’ve created an array repoItems := []repoStars{} which has multiple items of the struct above. This is how repoItems looks like: I’m trying to return those items as a JSON response: w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(repoItems) And it… Read More Go return struct as JSON in HTTP request

Slices return unexpected length

I’m studying Golang and I stopped by this and puzzled me. package main import "fmt" func main() { month := […]string{1: "Jan", 2: "Fab", 3: "March", 4: "April", 5: "May", 6: "June", 7: "July", 8: "Aug", 9: "Sep", 10: "Oct", 11: "Nov", 12: "Dec"} fmt.Println(cap(month)) summer := month[6:9] Q2 := month[4:7] fmt.Println(cap(Q2)) fmt.Println(len(Q2)) fmt.Println(cap(summer)) fmt.Println(len(summer))… Read More Slices return unexpected length

How to create simple Docker container with Go utilities installed

I’m kinda stuck exploring Docker features in order to create simple container with some Go utilities installed. I need to create image that has gosec and govulncheck utilities installed so I can run them on code in container. My petty attempt produced the following: # syntax=docker/dockerfile:1 FROM golang:1.19-alpine WORKDIR /app ENV GO111MODULE=on # copying my… Read More How to create simple Docker container with Go utilities installed

How to print struct as a plain string with escape characters in golang?

I am trying to print a Golang struct as a string with escape characters, but not able to do that. I want to print my struct like this: "{\"data\":\"MyName\",\"value\":\"Ashutosh\"}" Here is what I have tried. package main import ( "encoding/json" "fmt" ) type Resp struct { Data string `json:"data"` Value string `json:"value"` } func main()… Read More How to print struct as a plain string with escape characters in golang?