Bird
Raised Fist0
Goprogramming~20 mins

Go program structure - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Structure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of a simple Go program
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    fmt.Println("Hello, Go!")
}
ACompilation error
Bhello, go!
CHello Go!
DHello, Go!
Attempts:
2 left
๐Ÿ’ก Hint
Look at the exact string inside fmt.Println and remember Go is case-sensitive.
โ“ Predict Output
intermediate
2:00remaining
Variable declaration and output
What will this Go program print?
Go
package main
import "fmt"
func main() {
    var x int = 5
    fmt.Println(x)
}
ACompilation error
B5
C0
Dx
Attempts:
2 left
๐Ÿ’ก Hint
The variable x is assigned 5 before printing.
โ“ Predict Output
advanced
2:00remaining
Understanding package and import
What error does this Go program produce?
Go
package main
func main() {
    fmt.Println("Hi")
}
Aundefined: fmt
Bno error, prints Hi
Csyntax error: missing import
Druntime error
Attempts:
2 left
๐Ÿ’ก Hint
Check if the fmt package is imported before using it.
โ“ Predict Output
advanced
2:00remaining
Multiple functions and main execution
What is the output of this Go program?
Go
package main
import "fmt"
func greet() {
    fmt.Println("Hello")
}
func main() {
    greet()
    fmt.Println("World")
}
A
Hello
World
B
World
Hello
CHello World
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Functions run in the order they are called inside main.
๐Ÿง  Conceptual
expert
2:00remaining
Go program entry point and package rules
Which statement about Go program structure is correct?
AThe main() function can be in any package, not necessarily main.
BGo programs can run without a main() function if package main is present.
CEvery Go executable program must have package main and a main() function.
DPackages are optional in Go programs.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what Go requires to start running a program.

Practice

(1/5)
1. What is the purpose of the main function in a Go program?
easy
A. It is the entry point where the program starts running.
B. It declares the package name.
C. It imports external libraries.
D. It defines variables globally.

Solution

  1. Step 1: Understand the role of main function

    The main function is where the Go program begins execution.
  2. Step 2: Differentiate from other parts

    Package declaration and imports do not run code; only main runs the program.
  3. Final Answer:

    It is the entry point where the program starts running. -> Option A
  4. Quick Check:

    Main function = program start [OK]
Hint: Main function always starts the Go program [OK]
Common Mistakes:
  • Confusing package declaration with program start
  • Thinking imports run code
  • Assuming variables run the program
2. Which of the following is the correct way to start a Go program file?
easy
A. import "fmt"
B. func main() { }
C. package main
D. var main = 1

Solution

  1. Step 1: Identify the first line in a Go program

    Every Go program starts with a package declaration, usually package main for executable programs.
  2. Step 2: Check other options

    func main() is a function, not the first line. import comes after package. var main is invalid as a start.
  3. Final Answer:

    package main -> Option C
  4. Quick Check:

    Package declaration first = package main [OK]
Hint: Go files start with package declaration [OK]
Common Mistakes:
  • Starting with function before package
  • Using import before package
  • Declaring variables as first line
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
    fmt.Println("Hello, Go!")
}
medium
A. Hello, Go!
B. fmt.Println("Hello, Go!")
C. package main
D. Syntax error

Solution

  1. Step 1: Understand the code structure

    The program imports fmt and calls fmt.Println to print a string.
  2. Step 2: Predict the output

    The output will be the string inside Println: "Hello, Go!" printed on the screen.
  3. Final Answer:

    Hello, Go! -> Option A
  4. Quick Check:

    Println outputs string = Hello, Go! [OK]
Hint: Println prints the string inside quotes [OK]
Common Mistakes:
  • Confusing code with output
  • Expecting package or import to print
  • Thinking syntax error without reason
4. Identify the error in this Go program:
package main
import "fmt"
func main() {
    Println("Hello")
}
medium
A. Missing package declaration
B. Incorrect import syntax
C. Missing main function
D. Println should be fmt.Println

Solution

  1. Step 1: Check function call

    The code calls Println without prefix. In Go, Println is in fmt package, so it must be fmt.Println.
  2. Step 2: Verify other parts

    Package and import are correct. Main function exists. Import syntax is correct.
  3. Final Answer:

    Println should be fmt.Println -> Option D
  4. Quick Check:

    Use fmt.Println, not just Println [OK]
Hint: Always prefix Println with fmt. [OK]
Common Mistakes:
  • Calling Println without fmt prefix
  • Removing package declaration
  • Forgetting main function
5. You want to write a Go program that prints "Welcome!" but you forgot the package main line. What will happen when you try to run it?
hard
A. The program will run and print "Welcome!"
B. The compiler will show an error about missing package declaration
C. The program will print nothing but run successfully
D. The program will run but ignore the main function

Solution

  1. Step 1: Understand Go program requirements

    Every Go program must start with a package declaration, usually package main for executables.
  2. Step 2: Predict compiler behavior

    Without package main, the compiler will report an error and refuse to build the program.
  3. Final Answer:

    The compiler will show an error about missing package declaration -> Option B
  4. Quick Check:

    Missing package line causes compile error [OK]
Hint: Package main is mandatory for executable programs [OK]
Common Mistakes:
  • Assuming program runs without package
  • Thinking main function alone is enough
  • Ignoring compiler errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard