CodeBlanks Logo Icon CodeBlanks

Your first program

Write your first Go program with package main and fmt.Println.

Your first program

The entry point of every Go program is the main function inside the main package. To write an executable program you need a package main declaration, import "fmt" and func main().

fmt.Println prints one line to the screen. Text inside double quotes is printed as-is; a newline is appended automatically.

Go source files use the .go extension. You save your first program as hello.go and run it with go run hello.go.

package main and import

Every .go file starts with a package declaration. Executable programs use package main. Library packages are declared with their own name (e.g. package utils).

import "fmt" brings the standard library fmt package into scope. Multiple packages can be listed inside parentheses in a single import block.

You cannot use a package you have not imported, and importing a package you do not use is a compile error.

Practice Write Hello World

Add a fmt.Println line inside the empty main. The output must be Hello World.

Starter code

main.go
package main

import "fmt"

func main() {
    // buraya yaz
}

Sample solution

main.go
package main

import "fmt"

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

Expected output

Merhaba Dünya

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the entry point of a Go program?

  • the main function Correct answer
  • the start function
  • the run function
  • the init function

Explanation: The main function is the entry point of a Go program.

2 The fmt package

Question
package main

import "____"

func main() {
    fmt.Println("Hello")
}
Answer
package main

import "fmt"

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

Explanation: The fmt package provides printing to the screen.

3 Match the program parts

Term Meaning
package main Package declaration
import "fmt" Package import
func main() Entry function
fmt.Println Prints to screen

All lessons in Beginner