How can I pretty-print JSON using Go?

By pretty-print, I assume you mean indented, like so { “data”: 1234 } rather than {“data”:1234} The easiest way to do this is with MarshalIndent, which will let you specify how you would like it indented via the indent argument. Thus, json.MarshalIndent(data, “”, ” “) will pretty-print using four spaces for indentation.

Why would I make() or new()?

Go has multiple ways of memory allocation and value initialization: &T{…}, &someLocalVar, new, make Allocation can also happen when creating composite literals. new can be used to allocate values such as integers, &int is illegal: new(Point) &Point{} // OK &Point{2, 3} // Combines allocation and initialization new(int) &int // Illegal // Works, but it is … Read more

Handling JSON Post Request in Go

Please use json.Decoder instead of json.Unmarshal. func test(rw http.ResponseWriter, req *http.Request) { decoder := json.NewDecoder(req.Body) var t test_struct err := decoder.Decode(&t) if err != nil { panic(err) } log.Println(t.Test) }

How to get the directory of the currently running file?

EDIT: As of Go 1.8 (Released February 2017) the recommended way of doing this is with os.Executable: func Executable() (string, error) Executable returns the path name for the executable that started the current process. There is no guarantee that the path is still pointing to the correct executable. If a symlink was used to start … Read more

How to handle configuration in Go [closed]

The JSON format worked for me quite well. The standard library offers methods to write the data structure indented, so it is quite readable. See also this golang-nuts thread. The benefits of JSON are that it is fairly simple to parse and human readable/editable while offering semantics for lists and mappings (which can become quite … Read more

How to read/write from/to a file using Go

Let’s make a Go 1-compatible list of all the ways to read and write files in Go. Because file API has changed recently and most other answers don’t work with Go 1. They also miss bufio which is important IMHO. In the following examples I copy a file by reading from it and writing to … Read more

How can I read from standard input in the console?

I’m not sure what’s wrong with the block reader := bufio.NewReader(os.Stdin) fmt.Print(“Enter text: “) text, _ := reader.ReadString(‘\n’) fmt.Println(text) As it works on my machine. However, for the next block you need a pointer to the variables you’re assigning the input to. Try replacing fmt.Scanln(text2) with fmt.Scanln(&text2). Don’t use Sscanln, because it parses a string … Read more

go get results in ‘terminal prompts disabled’ error for github private repo

I found this extremely helpful, and it solved my problem. This command will allow your 2FA to do its thing (and save you the trouble of entering your username and password): For Github: git config –global –add url.”git@github.com:”.insteadOf “https://github.com/” For Gitlab: git config –global –add url.”git@gitlab.com:”.insteadOf “https://gitlab.com/” Source: http://albertech.blogspot.com/2016/11/fix-git-error-could-not-read-username.html If you’re not using 2FA, you … Read more

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