Hello, Go!

It is time to start using Go for real.

Before that, let us try a handy GoLand feature. Type just main.

GoLand suggestion

If you press Enter, GoLand automatically inserts the basic code you need.

Go is special in one way: it requires this line at the top of every program.

package main

We will explain why in the final part of this course, “Package Structure.” Now let us write some real code.

Type fmt. and GoLand will predict what you want, suggesting functions like Println. Press Enter and it will choose a gray item like Sprintf(). Press Tab and it will complete the faint Println("Hello World").

GoLand suggestion screen

Now type the code below exactly:

package main

import "fmt"

func main() {
    fmt.Println("Hello, Go!")
}

You just wrote your first program.


Wait, what does all of this mean?

  1. package main: A label that says, “This is the entry point of the program.”
  2. import “fmt”: Bring in the toolbox (fmt) that helps with printing. GoLand manages imports for you, so you can ignore it for now.
  3. func main(): The main magic box that runs first when the program starts.
  4. fmt.Println(“Hello, Go!”): Print “Hello, Go!” to the screen.

Now click the run button (>) next to func main, and choose Run.

GoLand run result

If you see Hello, Go!, you are already a Go developer. Change it to Hello, GoLand! and run it again.