Structs

We use structsarrow-up-right in Go to represent structured data. It's often convenient to group different types of variables together. For example, if we want to represent a car we could do the following:

type car struct {
	brand      string
	model      string
	doors      int
	mileage    int
}

This creates a new struct type called car. All cars have a brand, model, doors and mileage.

Structs in Go are often used to represent data that you might use a dictionary or object for in other languages.

1

Creating (instantiating) a struct

You can make a new car value in a few different ways:

myCar := car{
	brand:   "Toyota",
	model:   "Corolla",
	doors:   4,
	mileage: 25000,
}
  • This creates a new car struct.

  • The variable myCar now holds all that data together.

2

Option 2: Without field names (less common)

myCar := car{"Honda", "Civic", 2, 15000}

This works too, but you must follow the exact order the fields were defined in the struct (brand, model, doors, mileage).

If the struct changes later (like you add a new field), this version can break โ€” thatโ€™s why named fields are safer.

3

Option 3: Create an empty struct and fill it later

var myCar car
myCar.brand = "Ford"
myCar.model = "Focus"
myCar.doors = 4
myCar.mileage = 32000

This works if you want to create an empty car and fill it step-by-step.


Accessing struct fields

Once youโ€™ve created it, you can access its fields using the dot (.) operator:

fmt.Println(myCar.brand)   // Toyota
fmt.Println(myCar.mileage) // 25000

You can also modify them:

myCar.mileage += 1000
fmt.Println(myCar.mileage) // 26000

1

Example: Using it in a function

Output:


TL;DR โ€” Summary

Action
Example
Description

Define struct

type car struct {...}

Defines a new type

Create (instantiate)

myCar := car{brand: "Toyota", ...}

Creates a struct value

Access field

myCar.brand

Reads or writes a field

Pass to function

printCarInfo(myCar)

Structs can be passed by value

Modify field

myCar.mileage += 1000

Updates data inside struct

Calling a Struct in a Function

type car struct { brand string model string doors int mileage int }

1

Create a function that accepts a struct as a parameter

When you want to use your struct inside a function, you pass it in like any other variable โ€” you just give it the type of the struct (car in this case).

  • The parameter c is of type car.

  • Inside the function, you can access all of its fields with c.fieldName.

2

Create a car in main and call the function

Output:

3

Passing by value vs pointer (optional but important)

By default, Go passes structs by value โ€” meaning it sends a copy to the function. If you modify the struct inside the function, it wonโ€™t affect the original one unless you pass it by pointer.

Example โ€” by value (copy):

Example โ€” by pointer (reference):

4

Adding a method (bonus)

You can also attach a function directly to a struct type โ€” this is called a method.

Output:


TL;DR Summary

Concept
Example
Description

Pass struct to function

printCarInfo(myCar)

Sends a copy of the struct

Access inside function

c.brand, c.mileage

Use the dot (.) operator

Modify struct in function

func update(c *car)

Pass a pointer (*car) to change it

Method on struct

func (c *car) drive()

Attaches function directly to struct type