Wednesday, 5 June 2013

How to format golang code

Golang is a language developed at Google. More information can be found at the wikipedia page.

Go has an easy tool called gofmt  to format the code blocks of go program in easy readable format.

Here is the command to format the golang program using gofmt   and save it to the corresponding file.


gofmt -w helloworld.go

If we didn't use -w, it will write to formated code to standard output. If we use -w, it will save the formatted code to the corresponding file itself.

Before formatting:

cat helloworld.go
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}


After formatting:

cat helloworld.go
package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}



No comments:

Post a Comment