0
0
Goprogramming~5 mins

Type inference in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ADeclares a constant
BAssigns a value to an existing variable without type inference
CDeclares and initializes a variable with type inference
DImports a package
If you write x := 3.14 in Go, what type does Go infer for x?
Aint
Bstring
Cbool
Dfloat64
Can you write var age := 30 in Go?
ANo, := cannot be used with var
BNo, because 30 is not a valid value
CYes, but it declares a constant
DYes, it declares age with type inference
What type will Go infer for flag := true?
Abool
Bstring
Cint
Dfloat64
Why might you choose to use explicit types instead of type inference in Go?
ATo make the code longer
BTo clarify the variable's type for readability or specific needs
CBecause type inference is not supported
DTo avoid using variables
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.