Recall & Review
beginner
What is type inference in Go?
Type inference in Go means the compiler can figure out the type of a variable automatically based on the value assigned to it, so you don't always have to write the type explicitly.
Click to reveal answer
beginner
How do you declare a variable with type inference in Go?
You use the := operator to declare and initialize a variable without specifying its type. For example:
name := "Alice" lets Go infer that name is a string.Click to reveal answer
beginner
What happens if you assign a number to a variable using := in Go?
Go infers the variable's type as int if you assign an integer value, or float64 if you assign a decimal number. For example,
age := 30 makes age an int.Click to reveal answer
intermediate
Can you change the type of a variable after using type inference in Go?
No, once Go infers a variable's type, it cannot be changed. The variable keeps the inferred type for its entire lifetime.
Click to reveal answer
beginner
Why is type inference useful in Go?
Type inference makes code shorter and easier to read by removing the need to write types explicitly when they are obvious from the value. It helps beginners focus on logic rather than types.
Click to reveal answer
What does the := operator do in Go?
✗ Incorrect
The := operator declares a new variable and lets Go infer its type from the assigned value.
If you write
x := 3.14 in Go, what type does Go infer for x?✗ Incorrect
Go infers the type float64 for decimal numbers like 3.14.
Can you write
var age := 30 in Go?✗ Incorrect
The := operator is used without var. Using var and := together is a syntax error.
What type will Go infer for
flag := true?✗ Incorrect
Go infers the bool type for true or false values.
Why might you choose to use explicit types instead of type inference in Go?
✗ Incorrect
Explicit types can improve clarity or handle special cases where the inferred type is not what you want.
Explain how type inference works in Go and give an example of declaring a variable using it.
Think about how Go figures out the type without you writing it.
You got /3 concepts.
Describe one advantage and one limitation of using type inference in Go.
Consider what makes code simpler and what restrictions inference brings.
You got /2 concepts.