Printing Types
Part of the Fundamentals section of Coddy's GO journey — lesson 31 of 109.
In Go, we can display the type of a variable using the %T format verb with the fmt.Printf() function. Let's explore how to do this with different data types.
First, let's create variables of different types and print their types:
name := "Gopher"
age := 25
height := 1.85
isActive := true
fmt.Printf("name is type: %T\n", name)
fmt.Printf("age is type: %T\n", age)
fmt.Printf("height is type: %T\n", height)
fmt.Printf("isActive is type: %T\n", isActive)After executing this code, we'll see the type of each variable shown on the screen:
name is type: string
age is type: int
height is type: float64
isActive is type: boolThe %T format verb tells Go to display the data type instead of the value itself. This is very helpful when you're not sure what type a variable is or when you're learning about Go's type system.
Challenge
BeginnerIn this challenge, you'll practice using the %T format verb with fmt.Printf to display the types of different variables.
The code already has several variables of different types defined. Your task is to use fmt.Printf with the %T format verb to print out the type of each variable.
Cheat sheet
Use the %T format verb with fmt.Printf() to display the type of a variable:
name := "Gopher"
age := 25
height := 1.85
isActive := true
fmt.Printf("name is type: %T\n", name)
fmt.Printf("age is type: %T\n", age)
fmt.Printf("height is type: %T\n", height)
fmt.Printf("isActive is type: %T\n", isActive)Output:
name is type: string
age is type: int
height is type: float64
isActive is type: boolTry it yourself
package main
import "fmt"
func main() {
// These variables are already defined for you
age := 25
price := 19.99
isAvailable := true
productName := "Wireless Headphones"
// TODO: Use fmt.Printf with %T to print the type of the 'age' variable
// The output should be: "The type of age is: int"
// TODO: Use fmt.Printf with %T to print the type of the 'price' variable
// The output should be: "The type of price is: float64"
// TODO: Use fmt.Printf with %T to print the type of the 'isAvailable' variable
// The output should be: "The type of isAvailable is: bool"
// TODO: Use fmt.Printf with %T to print the type of the 'productName' variable
// The output should be: "The type of productName is: string"
}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