Skip to main content

Hello World

  1. Open text editor and create hello_world.go file and copy the below code.

    package main

    import "fmt"

    func main() {
    fmt.Println("Hello World")
    }
  2. Open terminal and change to directory where hello_world.go is present.

  3. Run go fmt hello_world.go to clean up the code formatting.

    go fmt hello_world.go
  4. Run go build hello_world.go to compile the source code.

    go build hello_world.go
  5. Run the executable file hello_world.exe (if on windows)

    Windows

    $ hello_world.exe
    Hello World

    Linux

    $ ./hello_world
    Hello World

Formatting Go Lang Code

  1. Importance and Advantage of go fmt.
    1. Go has this amazing support of go fmt.
    2. It auto formates code to get rid of extra spaces, fix indentations, and make the code better.
  2. There are also other tools like go vet, go test, etc which helps to improve code quality.