Creating Struct Instances
Part of the Fundamentals section of Coddy's GO journey — lesson 95 of 109.
After defining a struct type, you need to create instances (values) of that type to use in your program.
Define a Person struct:
type Person struct {
name string
age int
}
func main() {
// Create a Person instance
person1 := Person{name: "Alice", age: 30}
fmt.Println(person1)
}The output shows the struct instance with its field values:
{Alice 30}You can also create a struct with zero values by omitting fields:
person2 := Person{} // Creates {name:"", age:0}Challenge
BeginnerIn this challenge, you'll practice creating instances of a struct. A Person struct has already been defined with fields for name, age, and isStudent.
Your task is to create a new instance of the Person struct with the following information:
- Name: "Alice"
- Age: 25
- IsStudent: true
Then the program will print out the person's information.
Cheat sheet
To create instances of a struct, use the struct name followed by field values in curly braces:
type Person struct {
name string
age int
}
// Create a struct instance with field values
person1 := Person{name: "Alice", age: 30}
// Create a struct with zero values (empty fields)
person2 := Person{} // Creates {name:"", age:0}Try it yourself
package main
import "fmt"
// Person struct definition
type Person struct {
name string
age int
isStudent bool
}
func main() {
// TODO: Create a new Person struct instance with
// name: "Alice", age: 25, isStudent: true
// Don't modify the code below
fmt.Printf("Name: %s\n", person.name)
fmt.Printf("Age: %d\n", person.age)
fmt.Printf("Is Student: %t\n", person.isStudent)
}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 Actions10Composite Types: Arrays
Introduction to ArraysDeclaring ArraysInitializing ArraysAccessing Array ElementsArray Length with `len`Iterating Over ArraysMulti-dimensional Arrays13Composite Types: Structs
Defining Custom TypesCreating Struct InstancesAccessing Struct FieldsPointers to StructsInitializing StructsEmbedded StructsAnonymous StructsRecap - Custom Data Structures2Variables 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