CodeBlanks Logo Icon CodeBlanks

The main package

Learn that the main package is the program entry point.

The main package

In Go, the package named main is special: when the module containing it is compiled, an executable binary is produced. All other packages are libraries and cannot run directly.

The main package must contain func main(). When the program starts, this function is called. The main package can have multiple .go files; they all belong to the same package.

To produce the executable binary, you use the go build command.

Library packages

Packages other than main are libraries. For example, a file with package utils is part of the utils package. You use it from another package with import.

Library packages cannot run directly; they are only called by other packages. This lets you split your code into modules.

Exported (capital-letter) names in a package are reachable from other packages. Lowercase names stay package-private.

Practice The main package and function

Write the two requirements of an executable program: package main and func main(). In main, call a helper function named selam(); selam prints "program başladı".

Starter code

main.go
package main

import "fmt"

// selam fonksiyonunu ve main'i yaz

Sample solution

main.go
package main

import "fmt"

func selam() {
    fmt.Println("program başladı")
}

func main() {
    selam()
}

Expected output

program başladı

Questions and answers

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

1 Which function is required inside the main package?

  • func start()
  • func main() Correct answer
  • func run()
  • func init()

Explanation: The main package must contain func main().

2 Package declaration

Question
For an executable program:
____ main
Answer
For an executable program:
package main

Explanation: package main produces an executable program.

3 Match the package kinds

Term Meaning
main Executable
utils Library
fmt Standard library
github.com/x/y Third party

All lessons in Beginner