Go course

Packages and ModulesExported vs Unexported

Go

Display

Theme

Packages and Modules · Test exercise

Exported vs Unexported

Go has no public or private keywords. Visibility is decided by the first letter of a name:

NameStatusUsable from another package?
Hitung, Produk, MaxStokexported (uppercase)Yes
hitung, produk, maxStokunexported (lowercase)No, only within the same package

This rule applies to everything: functions, types, variables, constants, struct fields, and methods.

package akun

type Pengguna struct {
	Nama      string // exported: bisa dibaca/diubah package lain
	kataSandi string // unexported: hanya kode di package akun
}

func Baru(nama, sandi string) Pengguna {
	return Pengguna{Nama: nama, kataSandi: sandi}
}

func (p Pengguna) CekSandi(s string) bool { // exported method
	return p.kataSandi == s
}

Here Nama can be read and changed by other packages, while kataSandi (the password) is only visible to code in the akun package. From another package:

u := akun.Baru("rina", "rahasia")
fmt.Println(u.Nama)       // OK
fmt.Println(u.kataSandi)  // error kompilasi: u.kataSandi undefined
fmt.Println(u.CekSandi("rahasia")) // true

This pattern is called encapsulation: sensitive data is hidden, and access goes through functions or methods that you control.

Real effects in the standard library

Many standard packages that work with your structs (through reflection) can also only "see" exported fields. Take encoding/json: json.Marshal only includes exported fields in the JSON it produces.

type Kopi struct {
	Nama  string
	harga int
}

b, _ := json.Marshal(Kopi{Nama: "Latte", harga: 25000})
fmt.Println(string(b)) // {"Nama":"Latte"}

Note: the json.Marshal example above is for reading only. The Go runtime in Nibble includes unexported fields when it produces JSON (a runtime limitation), so this exercise doesn't use encoding/json. With Go installed on your own machine, the result is exactly what the comment above shows.

Since the exercises here are a single file (package main), every name can actually be accessed from anywhere in that file. The hidden tests behave like another package: they only use exported functions and methods.

Task

The profile page of a chat app shows a user summary. It turns out the password is showing too! Fix the Profil struct so that:

  • the Nama and Kota fields stay exported,
  • the password field becomes unexported, named sandi.

Then adjust the code:

  1. buatProfil and the CocokSandi method use the new field name.
  2. The Ringkasan() method returns <Nama> dari <Kota> ("Name from City") without the password. Example: buatProfil("Rina", "Bandung", "rahasia123").Ringkasan() produces Rina dari Bandung.
  3. The GantiSandi(lama, baru string) bool method changes the password and returns true only if lama (the old password) matches the current password and baru (the new one) isn't empty. Otherwise, the password doesn't change and the result is false.

That way, other packages can only check and change the password through methods you control.

main.go
package main

import "fmt"

type Profil struct {
	Nama  string
	Kota  string
	Sandi string // TODO: jadikan unexported dengan nama sandi
}

func buatProfil(nama, kota, sandi string) Profil {
	return Profil{Nama: nama, Kota: kota, Sandi: sandi}
}

func (p Profil) CocokSandi(s string) bool {
	return p.Sandi == s
}

// Ringkasan menampilkan profil di halaman publik.
func (p Profil) Ringkasan() string {
	// TODO: jangan tampilkan kata sandi
	return fmt.Sprintf("%s dari %s, sandi %s", p.Nama, p.Kota, p.Sandi)
}

// GantiSandi mengganti kata sandi jika sandi lama cocok dan sandi baru tidak kosong.
func (p *Profil) GantiSandi(lama, baru string) bool {
	// TODO
	return false
}

func main() {
	p := buatProfil("Rina", "Bandung", "rahasia123")
	fmt.Println(p.Ringkasan())
	fmt.Println("Ganti sandi:", p.GantiSandi("rahasia123", "lebihrahasia"))
	fmt.Println("Sandi baru cocok:", p.CocokSandi("lebihrahasia"))
}

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)