Menu
Coddy logo textTech

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 icon

Challenge

Beginner

In 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)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals