Bird
Raised Fist0
Goprogramming~20 mins

Package scope rules in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Package Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of accessing package-level variables
What is the output of this Go program?
Go
package main

import "fmt"

var packageVar = 10

func main() {
    fmt.Println(packageVar)
}
ACompilation error: packageVar undefined
B0
C10
DRuntime panic
Attempts:
2 left
💡 Hint
Package-level variables are accessible inside functions within the same package.
❓ Predict Output
intermediate
2:00remaining
Accessing unexported variable from another package
Given two packages, main and utils, where utils has an unexported variable, what happens when main tries to access it?
Go
package utils

var secret = 42

package main

import (
    "fmt"
    "utils"
)

func main() {
    fmt.Println(utils.secret)
}
A0
B42
CRuntime panic
DCompilation error: secret is not exported by package utils
Attempts:
2 left
💡 Hint
In Go, identifiers starting with lowercase letters are unexported and not accessible outside their package.
🔧 Debug
advanced
2:00remaining
Why does this code fail to compile?
Consider this Go code snippet. Why does it fail to compile?
Go
package main

import "fmt"

func main() {
    fmt.Println(helperVar)
}

var helperVar = 5
ACompilation error: undefined helperVar
BIt compiles and prints 5
CCompilation error: helperVar used before declaration
DRuntime panic: nil pointer dereference
Attempts:
2 left
💡 Hint
Package-level variables are initialized before main runs, regardless of their position in the file.
❓ Predict Output
advanced
2:00remaining
Shadowing package-level variable inside function
What is the output of this Go program?
Go
package main

import "fmt"

var count = 100

func main() {
    count := 50
    fmt.Println(count)
}
A50
B100
CCompilation error: count redeclared
D0
Attempts:
2 left
💡 Hint
A variable declared inside a function with := shadows the package-level variable.
❓ Predict Output
expert
3:00remaining
Package variable initialization order with init functions
What is the output of this Go program?
Go
package main

import "fmt"

var x = initializeX()

func initializeX() int {
    fmt.Println("initializeX called")
    return 42
}

func init() {
    fmt.Println("init function called")
}

func main() {
    fmt.Println("main function called")
    fmt.Println(x)
}
A
initializeX called
init function called
main function called
42
B
init function called
initializeX called
main function called
42
C
main function called
initializeX called
init function called
42
DCompilation error
Attempts:
2 left
💡 Hint
Package variables are initialized before init functions run.

Practice

(1/5)
1. In Go, what does it mean if a variable name starts with a lowercase letter inside a package?
easy
A. It is private to the package and cannot be accessed from other packages.
B. It is accessible from any package.
C. It is a constant value.
D. It is a global variable accessible everywhere.

Solution

  1. Step 1: Understand Go naming conventions

    In Go, identifiers starting with a lowercase letter are unexported, meaning they are private to the package.
  2. Step 2: Apply package scope rule

    Such variables cannot be accessed from outside their package, ensuring encapsulation.
  3. Final Answer:

    It is private to the package and cannot be accessed from other packages. -> Option A
  4. Quick Check:

    Lowercase start = private [OK]
Hint: Lowercase start means private inside package [OK]
Common Mistakes:
  • Thinking lowercase variables are accessible outside package
  • Confusing lowercase with constants
  • Assuming lowercase means global
2. Which of the following is the correct way to declare an exported function in a Go package?
easy
A. func MyFunction() {}
B. func myFunction() {}
C. func _MyFunction() {}
D. func my_function() {}

Solution

  1. Step 1: Recall Go export rules

    Functions starting with an uppercase letter are exported and accessible outside the package.
  2. Step 2: Check each option

    func MyFunction() {} starts with uppercase 'M', so it is exported. Others start with lowercase or invalid characters.
  3. Final Answer:

    func MyFunction() {} -> Option A
  4. Quick Check:

    Uppercase start = exported function [OK]
Hint: Function name uppercase means exported [OK]
Common Mistakes:
  • Using lowercase to export functions
  • Using underscores which are allowed but don't affect export
  • Confusing export with naming style
3. Given the following Go code in package 'mathops':
package mathops

var result = 10

func Result() int {
    return result
}
What will happen if another package tries to access mathops.result?
medium
A. It will access the value 10 successfully.
B. It will cause a compile-time error due to unexported variable.
C. It will cause a runtime panic.
D. It will access a zero value.

Solution

  1. Step 1: Identify variable scope

    The variable 'result' starts with lowercase, so it is unexported and private to the package.
  2. Step 2: Understand access from other packages

    Accessing 'mathops.result' from outside the package is not allowed and causes a compile-time error.
  3. Final Answer:

    It will cause a compile-time error due to unexported variable. -> Option B
  4. Quick Check:

    Lowercase variable = inaccessible outside package [OK]
Hint: Lowercase vars cause compile error if accessed outside [OK]
Common Mistakes:
  • Assuming private variables can be accessed
  • Thinking runtime panic occurs instead of compile error
  • Confusing function export with variable export
4. Consider this Go code snippet inside a package:
package example

func calculate() int {
    return 42
}

func Calculate() int {
    return calculate()
}
What is the main reason for defining both calculate and Calculate functions?
medium
A. To confuse the compiler with similar names.
B. To create two independent exported functions.
C. To cause a compile-time error due to duplicate names.
D. To have a private helper function and a public wrapper function.

Solution

  1. Step 1: Analyze function names and scope

    'calculate' starts lowercase, so it is private to the package; 'Calculate' starts uppercase, so it is exported.
  2. Step 2: Understand design pattern

    This pattern allows internal logic in 'calculate' while exposing a public function 'Calculate' that calls it.
  3. Final Answer:

    To have a private helper function and a public wrapper function. -> Option D
  4. Quick Check:

    Lowercase private + uppercase public wrapper [OK]
Hint: Use lowercase for private helpers, uppercase for public API [OK]
Common Mistakes:
  • Thinking both functions are exported
  • Expecting compile error from similar names
  • Assuming no reason for two functions
5. You want to create a Go package that exposes only a function GetData but keeps the data source variable private. Which code snippet correctly implements this using package scope rules?
hard
A. package data var datasource = "secret" func getData() string { return datasource }
B. package data var DataSource = "secret" func GetData() string { return DataSource }
C. package data var datasource = "secret" func GetData() string { return datasource }
D. package data var DataSource = "secret" func getData() string { return DataSource }

Solution

  1. Step 1: Identify private data variable

    The data source variable should start lowercase to be private to the package, so 'datasource' is correct.
  2. Step 2: Identify exported function

    The function 'GetData' starts uppercase, so it is exported and accessible outside the package.
  3. Step 3: Verify function returns private variable

    The function returns the private variable, exposing data safely.
  4. Final Answer:

    package data var datasource = "secret" func GetData() string { return datasource } -> Option C
  5. Quick Check:

    Lowercase var + uppercase func = private data, public access [OK]
Hint: Private vars lowercase, public funcs uppercase [OK]
Common Mistakes:
  • Making data variable exported by uppercase
  • Making function unexported by lowercase
  • Returning unexported function instead of exported
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