Bird
Raised Fist0
Goprogramming~5 mins

Type inference in Go - Cheat Sheet & Quick Revision

Choose your learning style10 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.

      Practice

      (1/5)
      1. What does type inference mean in Go when you write x := 10 inside a function?
      easy
      A. You must tell Go the type of x explicitly.
      B. Go automatically figures out that x is an int.
      C. Go treats x as a string by default.
      D. The variable x has no type.

      Solution

      1. Step 1: Understand the := operator in Go

        The := operator declares a variable and assigns a value, letting Go guess the type.
      2. Step 2: Check the assigned value type

        Since 10 is an integer, Go infers the type of x as int automatically.
      3. Final Answer:

        Go automatically figures out that x is an int. -> Option B
      4. Quick Check:

        Type inference means Go guesses type from value [OK]
      Hint: := means declare and infer type from value [OK]
      Common Mistakes:
      • Thinking you must always write the type explicitly
      • Assuming variables have no type
      • Believing Go defaults to string type
      2. Which of the following is the correct way to declare and assign a variable with type inference inside a function in Go?
      easy
      A. x := 5
      B. x = 5
      C. var x int = 5
      D. int x = 5

      Solution

      1. Step 1: Recall Go syntax for type inference

        Inside functions, := declares and assigns a variable with inferred type.
      2. Step 2: Check each option

        var x int = 5 declares with explicit type, not inference. x = 5 misses declaration. int x = 5 is invalid Go syntax.
      3. Final Answer:

        x := 5 -> Option A
      4. Quick Check:

        Use := to declare and infer type [OK]
      Hint: Use := inside functions for type inference [OK]
      Common Mistakes:
      • Using C or D which are invalid Go syntax
      • Forgetting to declare variable before assignment
      • Confusing var declaration with :=
      3. What is the output of this Go program?
      package main
      import "fmt"
      func main() {
          x := 3.14
          fmt.Printf("%T", x)
      }
      medium
      A. float64
      B. float32
      C. int
      D. string

      Solution

      1. Step 1: Identify the type inferred for 3.14

        In Go, untyped floating-point constants default to float64 when assigned with :=.
      2. Step 2: Understand fmt.Printf with %T

        The %T verb prints the type of the variable, so it will print "float64".
      3. Final Answer:

        float64 -> Option A
      4. Quick Check:

        Float literals default to float64 [OK]
      Hint: Float literals default to float64 type [OK]
      Common Mistakes:
      • Assuming float32 is default
      • Thinking 3.14 is int
      • Confusing %T output with value
      4. What is wrong with this Go code snippet?
      func main() {
          var x := 10
          fmt.Println(x)
      }
      medium
      A. The value 10 is not a valid integer.
      B. x is not declared before use.
      C. You cannot use := with var keyword.
      D. fmt.Println is not imported.

      Solution

      1. Step 1: Understand variable declaration syntax

        In Go, := is shorthand for declaration and assignment without var keyword.
      2. Step 2: Identify the syntax error

        Using var and := together is invalid syntax; you must use either var x = 10 or x := 10.
      3. Final Answer:

        You cannot use := with var keyword. -> Option C
      4. Quick Check:

        var and := cannot be combined [OK]
      Hint: Don't combine var and := in one statement [OK]
      Common Mistakes:
      • Mixing var and := together
      • Ignoring import errors (not shown here)
      • Thinking := works outside functions
      5. Given this Go code, what is the type of result after execution?
      func main() {
          a := 5
          b := 2.0
          result := a * int(b)
          fmt.Printf("%T", result)
      }
      hard
      A. float64
      B. string
      C. float32
      D. int

      Solution

      1. Step 1: Analyze variable types and conversion

        a is int, b is float64 (default for 2.0). The expression converts b to int before multiplication.
      2. Step 2: Determine result type

        Multiplying int (a) by int (converted b) results in int type for result.
      3. Final Answer:

        int -> Option D
      4. Quick Check:

        int * int = int type [OK]
      Hint: Type conversion affects result type [OK]
      Common Mistakes:
      • Assuming result is float64 without conversion
      • Forgetting to convert float to int
      • Confusing multiplication type rules
      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