Type Inference with `:=`
Part of the Fundamentals section of Coddy's GO journey — lesson 6 of 109.
Let's explore a convenient way to create and initialize values in Go using the := shorthand. This is called type inference, where Go automatically figures out what type your data should be.
To create a text message, use :=:
message := "Hello, Go!"
fmt.Println(message)After running this code, you'll see Hello, Go! printed to the console. Go automatically knew that message should be a string type.
Let's create a number using :=:
age := 25
fmt.Println(age)This prints 25 to the console. Go automatically made age an integer type.
Let's create a decimal number:
price := 19.99
fmt.Println(price)This prints 19.99 to the console. Go automatically made price a floating-point type.
Challenge
BeginnerIn this challenge, you'll practice using the := operator for type inference in Go.
The program already has some variables declared using the var keyword with explicit types. Your task is to declare two new variables using the := shorthand syntax that lets Go infer the types automatically.
Complete the TODOs in the code to make the program print the correct values.
Cheat sheet
Use the := shorthand for type inference in Go, where Go automatically determines the variable type:
Creating a string variable:
message := "Hello, Go!"
fmt.Println(message)Creating an integer variable:
age := 25
fmt.Println(age)Creating a floating-point variable:
price := 19.99
fmt.Println(price)Try it yourself
package main
import "fmt"
func main() {
// Variables declared with explicit types
var name string = "Gopher"
var age int = 5
// TODO: Declare a variable called 'language' with the value "Go" using := syntax
language ? "Go"
// TODO: Declare a variable called 'version' with the value 1.18 using := syntax
version ? 1.18
// Print all variables
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Language:", language)
fmt.Println("Version:", version)
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions