Bird
Raised Fist0
Goprogramming~10 mins

Go compilation and execution flow - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print "Hello, Go!" to the console.

Go
package main

import "fmt"

func main() {
    fmt.[1]("Hello, Go!")
}
Drag options to blanks, or click blank then click option'
APrint
BPrintln
CPrintf
DPrintfln
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using Print instead of Println will not add a new line.
Using Printfln is not a valid function.
2fill in blank
medium

Complete the code to declare a variable named "count" of type int with value 10.

Go
package main

func main() {
    var count [1] = 10
}
Drag options to blanks, or click blank then click option'
Astring
Bbool
Cint
Dfloat64
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using string type for a number value.
Using float64 when an integer is enough.
3fill in blank
hard

Fix the error in the code to correctly compile and run.

Go
package main

[1] "fmt"

func main() {
    fmt.Println("Go is fun!")
}
Drag options to blanks, or click blank then click option'
Aimport
Bpackage
Cfunc
Dvar
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using package instead of import.
Trying to declare fmt as a variable.
4fill in blank
hard

Fill both blanks to create a map from string to int with two entries.

Go
package main

func main() {
    scores := map[string][1]{
        "Alice": [2],
        "Bob": 90,
    }
}
Drag options to blanks, or click blank then click option'
Aint
B85
Cstring
D100
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using string as value type instead of int.
Using a string value for Alice's score.
5fill in blank
hard

Fill all three blanks to define a function that returns the sum of two integers.

Go
package main

func [1](a, b [2]) [3] {
    return a + b
}
Drag options to blanks, or click blank then click option'
Aadd
Bint
Dsum
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different types for parameters and return.
Using a function name that does not match the action.

Practice

(1/5)
1. What does the go build command do in Go programming?
easy
A. It compiles the code and creates an executable file.
B. It runs the code without compiling.
C. It only checks the code for errors without compiling.
D. It deletes the executable file.

Solution

  1. Step 1: Understand the purpose of go build

    The go build command compiles Go source code into an executable binary file.
  2. Step 2: Differentiate from other commands

    Unlike go run, go build does not run the program immediately but creates a file you can run later.
  3. Final Answer:

    It compiles the code and creates an executable file. -> Option A
  4. Quick Check:

    go build creates executable [OK]
Hint: Remember: build = compile and save executable [OK]
Common Mistakes:
  • Confusing go build with go run
  • Thinking go build runs the program immediately
  • Believing go build only checks syntax
2. Which of the following is the correct way to compile and run a Go program in one step?
easy
A. go execute main.go
B. go build main.go
C. go run main.go
D. go compile main.go

Solution

  1. Step 1: Identify the command that compiles and runs

    go run compiles the code and runs it immediately without creating an executable file.
  2. Step 2: Check other options

    go build only compiles; go execute and go compile are not valid Go commands.
  3. Final Answer:

    go run main.go -> Option C
  4. Quick Check:

    go run compiles and runs [OK]
Hint: Use go run to compile and run instantly [OK]
Common Mistakes:
  • Using go build to run directly
  • Typing invalid commands like go execute
  • Confusing compile-only and run commands
3. What will happen if you run the command go run main.go on a valid Go source file?
medium
A. It compiles the code, creates an executable file, and runs it.
B. It deletes the source file after running.
C. It only checks the code for errors but does not run it.
D. It compiles the code and runs it without creating an executable file.

Solution

  1. Step 1: Understand go run behavior

    go run compiles the source code in memory and runs it immediately without saving an executable file.
  2. Step 2: Eliminate incorrect options

    It compiles the code, creates an executable file, and runs it. is wrong because no executable file is saved. It only checks the code for errors but does not run it. is wrong because it runs the program. It deletes the source file after running. is incorrect as source files are never deleted.
  3. Final Answer:

    It compiles the code and runs it without creating an executable file. -> Option D
  4. Quick Check:

    go run compiles & runs, no file saved [OK]
Hint: Run with go run to skip saving executable [OK]
Common Mistakes:
  • Assuming go run saves an executable
  • Thinking go run only checks syntax
  • Believing source files get deleted
4. You run go build on your Go program, but no executable file appears. What is the most likely reason?
medium
A. Go automatically deletes the executable after building.
B. The source file has syntax errors preventing compilation.
C. The executable is created but hidden in a different directory.
D. The go build command only runs the program, not build it.

Solution

  1. Step 1: Understand go build behavior on errors

    If the source code has syntax errors, go build will fail and not create an executable file.
  2. Step 2: Check other options

    go build does not run the program, executables are created in the current directory by default, and Go does not delete executables automatically.
  3. Final Answer:

    The source file has syntax errors preventing compilation. -> Option B
  4. Quick Check:

    Syntax errors block build, no executable made [OK]
Hint: Check for syntax errors if no executable appears [OK]
Common Mistakes:
  • Expecting go build to run the program
  • Looking in wrong directories without checking errors
  • Assuming Go deletes executables automatically
5. You want to quickly test a small Go program without creating an executable file. Which command should you use and why?
hard
A. Use go run because it compiles and runs the program without saving an executable.
B. Use go build because it compiles and runs the program immediately.
C. Use go install because it runs the program without compiling.
D. Use go test because it compiles and runs the program.

Solution

  1. Step 1: Identify the command for quick testing

    go run compiles and runs the program immediately without creating an executable file, perfect for quick tests.
  2. Step 2: Understand why other commands don't fit

    go build creates an executable but does not run it. go install compiles and installs packages, not for quick runs. go test runs tests, not general programs.
  3. Final Answer:

    Use go run because it compiles and runs the program without saving an executable. -> Option A
  4. Quick Check:

    Quick test = go run [OK]
Hint: Use go run for quick compile-and-run tests [OK]
Common Mistakes:
  • Using go build expecting immediate run
  • Confusing go install with running code
  • Trying go test for normal program execution
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