Tsallaka zuwa abun ciki
Justice Tech School

Structs and Methods

A struct groups fields. Methods attach to a type with a receiver, and pointer receivers (*T) can change the value. Go has no classes, but this covers the same needs.

Example
package main

import "fmt"

type Student struct {
	Name  string
	Score int
}

func (s *Student) Add(p int) { s.Score += p }
func (s Student) Describe() string { return fmt.Sprintf("%s: %d", s.Name, s.Score) }

func main() {
	s := Student{Name: "Ada", Score: 70}
	s.Add(5)
	fmt.Println(s.Describe())
}

Result

Ada: 75

Exercise

Declare a struct.

type Point  { X int }

Quiz Test

Does Go have classes?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.