Hello World
-
Open text editor and create hello_world.go file and copy the below code.
package main
import "fmt"
func main() {
fmt.Println("Hello World")
} -
Open terminal and change to directory where hello_world.go is present.
-
Run
go fmt hello_world.go
to clean up the code formatting.go fmt hello_world.go
-
Run
go build hello_world.go
to compile the source code.go build hello_world.go
-
Run the executable file hello_world.exe (if on windows)
Windows
$ hello_world.exe
Hello WorldLinux
$ ./hello_world
Hello World
Formatting Go Lang Code
- Importance and Advantage of
go fmt
.- Go has this amazing support of go fmt.
- It auto formates code to get rid of extra spaces, fix indentations, and make the code better.
- There are also other tools like go vet, go test, etc which helps to improve code quality.