Writing first Go program - Time & Space Complexity
When we write our first Go program, it is important to see how the program's steps grow as we add more work.
We want to know how the time to run changes when the program handles more data or tasks.
Analyze the time complexity of the following code snippet.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
This code prints a simple greeting message once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Printing a message once.
- How many times: Exactly one time.
Since the program prints only once, the work does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of steps stays the same no matter how big the input is.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how much data it might handle.
[X] Wrong: "Printing a message once takes longer if the program is bigger."
[OK] Correct: The program only prints once, so the size of the program or input does not change the time it takes.
Understanding simple programs helps build confidence and shows you can think about how programs grow in time.
"What if we added a loop to print the message multiple times? How would the time complexity change?"
Practice
main function in a Go program?Solution
Step 1: Understand the role of
Themainfunctionmainfunction is special in Go; it is where the program starts running.Step 2: Differentiate from other parts
Packages define code grouping, imports bring libraries, and variables store data, but none start execution.Final Answer:
It is the starting point where the program begins execution. -> Option AQuick Check:
Program starts at main() -> D [OK]
- Confusing package declaration with program start
- Thinking imports start the program
- Assuming variables control program flow
Solution
Step 1: Identify the correct print function in Go
Go uses thefmtpackage withPrintlnto print with a newline.Step 2: Check syntax correctness
fmt.Println("Hello, Go!")is correct; others are either wrong function names or from other languages.Final Answer:
fmt.Println("Hello, Go!") -> Option AQuick Check:
Use fmt.Println() to print lines in Go [OK]
- Using fmt.print instead of fmt.Println
- Using print() which is not standard in Go
- Using console.log from JavaScript
package main
import "fmt"
func main() {
fmt.Println("Welcome to Go!")
}Solution
Step 1: Understand program structure
The program imports fmt and defines main(), which prints the string.Step 2: Identify output of fmt.Println
It prints the string exactly: Welcome to Go! with a newline.Final Answer:
Welcome to Go! -> Option CQuick Check:
fmt.Println prints string output [OK]
- Confusing code with output
- Expecting code to print itself
- Thinking main function is missing
package main
import "fmt"
func main() {
fmt.Println("Hello, world!"
}Solution
Step 1: Check syntax of fmt.Println
The call to fmt.Println is missing a closing parenthesis at the end.Step 2: Verify other parts
Package and import are present, function name is correct, so no errors there.Final Answer:
Missing closing parenthesis in fmt.Println call -> Option DQuick Check:
Check parentheses in function calls [OK]
- Forgetting closing parenthesis in function calls
- Assuming missing import or package causes this error
- Misnaming main function
Solution
Step 1: Understand printing with new lines
fmt.Printlnprints text and adds a newline automatically.Step 2: Check each option's output
A: Twofmt.Printlncalls print "Hello" on one line and "World" on the next. Correct.
B:fmt.Print("Hello\nWorld")prints "Hello\nWorld" (with actual newline) on one line.
C:fmt.Println("Hello\nWorld")prints "Hello\nWorld" (with actual newline) followed by newline, so output is two lines.
D: Twofmt.Printcalls print "HelloWorld" on one line.Final Answer:
package main import "fmt" func main() { fmt.Println("Hello") fmt.Println("World") } -> Option BQuick Check:
Use fmt.Println twice for separate lines [OK]
- Using fmt.Print without newline causes joined output
- Expecting \n inside fmt.Println to create new line
- Confusing fmt.Print and fmt.Println behavior
Ready to prove it?
Pass the test and this pattern moves unknown
