Aloptrbl - Devlog

GO Bahagian I: Asas

Pengenalan

Golang, atau GO adalah bahasa pemrograman yang direka untuk kelajuan dan kecekapan. Google introduce bahasa ni pada tahun 2009.

Kenapa Golang?

  1. Cepat & Efisyen: Golang ni memang laju. Dia guna memori dengan efisyen dan boleh buat banyak kerja serentak (concurrency) dengan goroutines.

  2. Mudah untuk Belajar: Syntax dia simple dan straightforward. Confirm takde complicated stuff.

  3. Kuat: Golang ada banyak ciri-ciri best seperti goroutines, channels, dan built-in support untuk web development.

Asas-asas Golang

Ni pulak basics yang kau kene tau:

Variables

Dalam Golang, kau boleh declare variable camni:

var name string = "Ali"

atau lagi ringkas:

name := "Ali"

Data Types

  1. Boolean: true dan false
  2. Numeric: int, float, complex
  3. String: Urutan byte. Contoh: "Hello World"
  4. Array: Array item yang sama. Saiznya fixed.
  5. Slice: Array yang saiz boleh change.
  6. Map: Pair key-value.
  7. Struct: Kumpulan data berbeza dalam satu unit.
  8. Pointer: Alamat memori dari nilai lain.
  9. Interface: Menentukan methods yang wajib diimplementasikan.
  10. Function: Block code yang boleh dipanggil selalu.
  11. Channel: Untuk communication antara goroutine.

Operators

a := 10
b := 20
c := a + b

Control Flow

Boleh guna if, else dan switch:

if condition {
    statement
} else if condition {
    statement
} else {
    statement
}
switch condition {
    case 1:
        statement
    case 2:
        statement
    default:
        statement
}

Loop

Dalam Golang,just guna for je:

for i := 0; i < 10; i++ {
    statement
}
for {
    statement
}
for i := 0; i < 10; i++ {
    if condition {
        break
    }
}
for range slice {
    statement
}
for range map {
    statement
}
for range channel {
    statement
}

Function

Function dalam Golang:

func functionName() {
    statement
}
func functionName(param1, param2 int) int {
    statement
    return result
}
func functionName() int {
    statement
    return result
}

Arrays & Slices

Array fixed size, Slice flexible:

array := [5]int{1, 2, 3, 4, 5}
slice := array[1:3]
var fruits [3]string
fruits[0] = "Apple"
fruits[1] = "Banana"
fruits[2] = "Cherry"

numbers := []int{1, 2, 3, 4, 5}

Maps

Map untuk key-value pairs:

map := make(map[string]int)
map["apple"] = 1
map["banana"] = 2
map["cherry"] = 3
map := map[string]int{
    "apple": 1,
    "banana": 2,
    "cherry": 3,
}

Struct

Struct untuk kumpulan data berbeza:

type person struct {
    name string
    age int
}
person := person{
    name: "John",
    age: 30,
}
person := person{name: "John", age: 30}

Pointers

Pointer showing alamat memori:

var a int = 10
var b *int = &a
a := 10
b := &a
a := 10
b := &a
*b = 20
a := 10
b := &a
c := *b

Interfaces

Interface sebagai blueprint:

type animal interface {
    eat()
    sleep()
}
type dog struct {
    name string
}

func (d dog) eat() {
    fmt.Println("Dog is eating")
}

Channels

Channel untuk communication antara goroutine:

ch := make(chan int)
ch := make(chan int, 5)
ch <- 1
x := <-ch
ch := make(chan int)
go func() {
    ch <- 1
}()
x := <-ch

References

https://go.dev/doc/go_fundamentals