0
0
Goprogramming~20 mins

main package and main function in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Main Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of a simple main function
What is the output of this Go program?
Go
package main

import "fmt"

func main() {
    fmt.Println("Hello, Go!")
}
ANo output
BHello, Go!
Cfmt.Println("Hello, Go!")
Dhello, go!
Attempts:
2 left
๐Ÿ’ก Hint
Look at what fmt.Println prints exactly.
โ“ Predict Output
intermediate
2:00remaining
What happens if main package is missing?
What error will this Go program produce?
Go
package main
Acompile error: no main function in main package
Bruntime error: missing main package
Cruns with no output
Dcompiles and prints nothing
Attempts:
2 left
๐Ÿ’ก Hint
The Go compiler requires a main package with main function to run an executable.
๐Ÿ”ง Debug
advanced
2:00remaining
Why does this program not run?
This Go program compiles but does not run as expected. Why?
Go
package main

func Main() {
    // empty
}
AFunction name must be 'main' with lowercase m
BPackage name must not be 'main'
CMain function must return int
DMissing import "fmt"
Attempts:
2 left
๐Ÿ’ก Hint
Go is case sensitive for function names.
๐Ÿง  Conceptual
advanced
2:00remaining
Role of main package in Go
Which statement best describes the role of the main package in Go?
AIt runs tests automatically
BIt is used only for library code
CIt automatically imports all other packages
DIt defines an executable program entry point
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens when you run 'go run' or 'go build'.
โ“ Predict Output
expert
3:00remaining
Output of multiple main functions in different files
Given two files in the same main package, what is the output when running the program? File1.go: package main import "fmt" func main() { fmt.Println("Start") helper() } File2.go: package main import "fmt" func helper() { fmt.Println("Helper called") }
Acompile error: multiple main functions
B
Helper called
Start
C
Start
Helper called
Druntime error: undefined helper
Attempts:
2 left
๐Ÿ’ก Hint
Functions in the same package can call each other.