Go course

StructsMethods on Structs

Go

Display

Theme

Structs · Test exercise

Methods on Structs

A method is a function that's "attached" to a type. The only difference from a regular function is the receiver — a special parameter written before the method name:

type Persegi struct {
	Sisi float64
}

func (p Persegi) Luas() float64 {
	return p.Sisi * p.Sisi
}

func (p Persegi) Keliling() float64 {
	return 4 * p.Sisi
}

Here Persegi is a square with a side length (Sisi), and the methods compute its area (Luas) and perimeter (Keliling).

You call a method with a dot, just like accessing a field:

kotak := Persegi{Sisi: 3}
fmt.Println(kotak.Luas())     // 9
fmt.Println(kotak.Keliling()) // 12

The receiver name is usually short — one or two letters from the type name (p for Persegi).

Value receivers work on a copy

The receiver (p Persegi) is called a value receiver: the method gets a copy of the struct. Changing a field inside the method doesn't affect the original struct:

func (p Persegi) Perbesar() {
	p.Sisi = p.Sisi * 2 // hanya mengubah salinan!
}

kotak.Perbesar()
fmt.Println(kotak.Sisi) // tetap 3

Perbesar ("enlarge") only changes the copy, so kotak.Sisi is still 3.

With a value receiver, the way to "change" something is to return a new value:

func (p Persegi) Diperbesar() Persegi {
	p.Sisi = p.Sisi * 2
	return p
}

besar := kotak.Diperbesar() // besar.Sisi == 6, kotak.Sisi tetap 3

(Methods that really change the original struct use a pointer receiver, covered in the Pointers chapter.)

Methods and the zero value

You can still call a method on a struct's zero value. Good design makes sure the zero value still produces something sensible:

var p Persegi
fmt.Println(p.Luas()) // 0

Task

The "Senja" coffee shop records orders with the Pesanan (order) type. Complete three methods (all with value receivers):

  1. (p Pesanan) Total() int — the subtotal is Harga * Jumlah (price times quantity). If Member is true, apply a 10% discount: subtotal * 90 / 100.

  2. (p Pesanan) Struk() string — a receipt line in this format:

    <Jumlah>x <Menu> = Rp<Total>

    Example: 2x Latte = Rp44000 (use the result of the Total method).

  3. (p Pesanan) TambahJumlah(n int) Pesanan — return a new order with Jumlah increased by n. The original order must not change.

main.go
package main

import "fmt"

type Pesanan struct {
	Menu   string
	Harga  int
	Jumlah int
	Member bool
}

func (p Pesanan) Total() int {
	// TODO: Harga * Jumlah, diskon 10% untuk member
	return p.Harga
}

func (p Pesanan) Struk() string {
	// TODO: "<Jumlah>x <Menu> = Rp<Total>"
	return p.Menu
}

func (p Pesanan) TambahJumlah(n int) Pesanan {
	// TODO: kembalikan salinan dengan Jumlah bertambah n
	return p
}

func main() {
	latte := Pesanan{Menu: "Latte", Harga: 22000, Jumlah: 2}
	fmt.Println(latte.Struk())

	lebih := latte.TambahJumlah(1)
	fmt.Println(lebih.Struk())
	fmt.Println(latte.Struk())

	var kosong Pesanan
	fmt.Println("Total pesanan kosong:", kosong.Total())
}

Output

Go runtime not loaded yet

Press Run to see the output, or Submit to check your code.

Ctrl/Cmd + Enter: Run · Ctrl/Cmd + Shift + Enter: Submit · Ctrl/Cmd + S: tidy the code (gofmt)