Go course

VariablesBasic Data Types

Go

Display

Theme

Variables · Code example

Basic Data Types

Go is a statically typed language: the type of every variable is known at compile time and can't change.

These are the basic types you'll use most often:

bool

string

int  int8  int16  int32  int64      // bilangan bulat bertanda
uint uint8 uint16 uint32 uint64     // bilangan bulat tanpa tanda

byte  // alias untuk uint8
rune  // alias untuk int32, mewakili satu karakter Unicode

float32 float64   // bilangan pecahan

complex64 complex128

The int family is signed integers, the uint family is unsigned integers, byte is an alias for uint8, rune is an alias for int32 that represents a single Unicode character, and float32/float64 hold fractional numbers.

Rule of thumb: use int, float64, string, and bool unless you have a specific reason not to (for example, saving memory or working with a binary format).

Type conversion

Go doesn't convert between number types automatically. You have to convert explicitly:

suhu := 36.6         // float64
bulat := int(suhu)   // 36 — bagian desimal dibuang
f := float64(bulat)  // 36.0

Converting 36.6 to int gives you 36 — the decimal part is simply dropped.

Try it yourself

The editor has an example you can play with. Press Run (or Ctrl/Cmd + Enter). Also try removing float64(...) from the average calculation and see the compile error you get.

main.go
package main

import "fmt"

func main() {
	var totalNilai int = 255
	jumlahSiswa := 3

	rataRata := float64(totalNilai) / float64(jumlahSiswa)

	var huruf rune = 'G'
	var b byte = 'o'

	fmt.Println("Rata-rata:", rataRata)
	fmt.Println("Dibulatkan ke bawah:", int(rataRata))
	fmt.Println("Rune:", huruf, string(huruf))
	fmt.Println("Byte:", b, string(b))
	fmt.Printf("Tipe rataRata: %T\n", rataRata)
}

Output

Go runtime not loaded yet

Press Run to see what the program prints.

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