Go Programming Practice & Conceptual Questions
This section provides practice and conceptual questions for foundational Go topics, designed to deepen your understanding for real-world, cloud-native development.
1. Hello World
Write a program to print "Hello, Go!" to the console.
Modify the program to take the user's name as input and greet them with "Hello, [Name]!".
Write a program to print a multiline string using a single
fmt.Println
statement.What is the purpose of the
main
package andmain()
function in Go?How does Go handle Unicode and UTF-8 in string literals?
2. Variables and Constants
Declare variables of type
int
,float64
, andstring
, and initialize them with values. Print the values and their types.Create a constant for Pi (3.14159) and use it to calculate the circumference of a circle with a given radius.
Demonstrate the difference between
var
declaration and short variable declaration (:=
).What are zero values in Go? Give examples for different types.
Use
iota
to define an enum for log levels: DEBUG, INFO, WARN, ERROR.Why does Go require explicit type conversion? Show an example where implicit conversion would cause a bug.
Write a function that takes a string and returns its rune count and Unicode code points.
What is the difference between a constant and a variable in Go? When should you use each?
3. Control Statements
Write a program to check whether a given number is even or odd.
Create a program to print all numbers from 1 to 100 that are divisible by 3 using a
for
loop.Use a
switch
statement to print the day of the week based on a number input (1 for Monday, 2 for Tuesday, etc.).Write a program to calculate the factorial of a number using a loop.
Write a program to classify a grade into A/B/C/Fail using if-else.
Check if a number is positive, negative, or zero.
Print the Fibonacci series up to N terms using a loop.
Sum even numbers from 1 to 50 using continue.
Use fallthrough in a switch to group weather types: Sunny, Cloudy, Rainy.
Replace an if-else ladder for marks grading with a switch statement.
What are the benefits of using switch over multiple if-else statements?
4. Functions
Write a function that takes two integers and returns their sum, difference, product, and quotient.
Create a recursive function to calculate the Fibonacci sequence up to the nth term.
Write a function that swaps two numbers using pointers.
Define and use an anonymous function to calculate the square of a number.
Modify a variadic function to return both the sum and average of its arguments.
Write a closure that accumulates a running total.
Create a function type and use it to pass different operations (add, subtract, multiply, divide) to a higher-order function.
What are the advantages of using closures in Go?
How does Go handle multiple return values? Give a practical example.
5. Arrays, Slices, and Maps
Create an array of 5 integers and calculate their sum.
Write a program to append elements to a slice dynamically and print its length and capacity after each addition.
Create a map to store student names as keys and their grades as values. Add, retrieve, and delete entries from the map.
Write a program to find the largest and smallest numbers in a slice.
Create an array of strings and print each character in reverse order.
Use append and copy to merge two slices.
Create a map of countries with nested maps of cities and populations.
Create a slice, append till it doubles its capacity and print it at each step.
Summarize pros and cons of arrays, slices, and maps in your own words.
What are the differences between value types and reference types in Go? How does this affect arrays, slices, and maps?
6. Structs and Methods
Define a struct
Person
with fieldsName
,Age
, andCity
. Create an instance and print its details.Add a method to the
Person
struct to print a greeting message like "Hi, I'm [Name] from [City]".Create a struct
Rectangle
with fieldsLength
andWidth
. Add a method to calculate and return its area.Implement a pointer receiver method to update the
Age
of aPerson
struct.Create a new struct called
Car
with fields Make, Model, and Year.Add a method to
Car
calledStart
that prints a message using the car's Make and Model.Create a new struct called
Garage
that contains a slice of Cars and a method to add new cars.Modify the
Person
struct to include an Email field and update the Greet method to use it.Compare two Employee structs and explain when two structs are considered equal in Go.
What is struct embedding and how does it differ from inheritance in other languages?
7. Pointers
Write a program to demonstrate how a pointer can modify the value of a variable.
Create a function that increments a number using a pointer.
Define a pointer to a struct and modify its fields using the pointer.
Demonstrate a pointer to a pointer in Go and print all dereferenced values.
Write a function
swap(a, b *int)
that swaps two integers using pointers.Create a struct
Rectangle
with Width and Height. Write a method using pointer receiver to scale its size.Declare a nil pointer to a float64 and safely assign it a value inside a function.
Use a pointer to update the second element in a slice of strings.
Declare a variable of type pointer to pointer (
**int
) and print the dereferenced value.(Advanced) Create a function that takes a slice and modifies it using a pointer to the first element.
Why does Go disallow pointer arithmetic? What are the safety benefits?
8. Errors in Go
Write a function that returns an error if a file does not exist.
Create a function that divides two numbers and returns an error if the divisor is zero. Demonstrate idiomatic error checking.
Use
fmt.Errorf
to wrap an error with context and extract the original error usingerrors.Is
.Define a sentinel error and show how to compare it using
errors.Is
.Create a custom error type (e.g., for HTTP errors) and demonstrate type assertion with
errors.As
.Refactor a function to propagate errors up the call stack with context using
%w
.Show how to use
defer
for resource cleanup in the presence of errors.List and explain at least three anti-patterns in Go error handling.
Compare Go’s error handling to exception-based languages. What are the trade-offs?
Write a real-world style function that fetches data, processes it, and saves it, handling and propagating errors at each step.
General Challenges
Combine all concepts to create a mini project:
Define a
Student
struct with fieldsName
,Subjects
, andGrades
(map of subject to grade).Write functions to:
Add a subject and grade.
Calculate the average grade.
Print the student's details in a formatted way.
Write a program to manage an inventory system:
Use a map to store item names as keys and their quantities as values.
Add functions to add items, update quantities, and display the inventory.
Create a program that simulates a basic calculator supporting addition, subtraction, multiplication, and division, implemented using functions.
Build a RESTful API in Go (using net/http) to manage a list of books (CRUD operations). (Bonus: Use structs, slices, and maps.)
Design a CLI tool in Go that reads a CSV file and summarizes the data (e.g., average, min, max for numeric columns).
Additional Notes
Focus on proper formatting and naming conventions.
Experiment with error handling where applicable.
Use comments to explain your code.
Try to relate each concept to real-world, cloud-native scenarios (e.g., config management, API design, data processing).
Last updated