Skip to content

Commit 084265b

Browse files
committed
update readme
1 parent 4ac0396 commit 084265b

File tree

11 files changed

+619
-85
lines changed

11 files changed

+619
-85
lines changed

channel/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
// Membuat channel yang dapat mengirim dan menerima nilai bertipe string
9+
message := make(chan string)
10+
11+
// Fungsi anonim untuk mengirim pesan melalui channel
12+
var SayMyName = func(who string) {
13+
data := fmt.Sprintf("Hai %s", who)
14+
message <- data // Mengirim data ke channel
15+
}
16+
17+
// Menjalankan beberapa goroutine
18+
go SayMyName("Jhon")
19+
go SayMyName("Tom")
20+
go SayMyName("Hardy")
21+
22+
// Menerima pesan dari channel sebanyak jumlah goroutine
23+
// Hasil bisa saja tidak berurutan karena goroutine berjalan secara paralel
24+
for i := 0; i < 3; i++ {
25+
fmt.Println(<-message)
26+
}
27+
28+
// Menutup channel karena sudah tidak digunakan lagi
29+
close(message)
30+
}

goroutine/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
/*
4+
Goroutine di Go sangat berguna dalam berbagai situasi yang membutuhkan concurrency (eksekusi bersamaan) atau paralelisme (pekerjaan yang dibagi di beberapa core atau thread),
5+
tanpa membuat aplikasi menjadi kompleks atau memakan banyak sumber daya.
6+
*/
7+
8+
import (
9+
"fmt"
10+
"time"
11+
)
12+
13+
// Fungsi LoopingPrint mencetak pesan sebanyak 'number' kali
14+
func LoopingPrint(number int, message string) {
15+
for i := 0; i < number; i++ {
16+
fmt.Println(i+1, message) // Mencetak nomor urut dan pesan
17+
}
18+
}
19+
20+
// Struktur Person yang memiliki dua field: ID dan Name
21+
type Person struct {
22+
ID int
23+
Name string
24+
}
25+
26+
// Metode SayMyName mengembalikan string yang berisi nama orang
27+
func (p Person) SayMyName() string {
28+
return p.Name + " dari " + p.Name
29+
}
30+
31+
// Metode CallMe mencetak ID dan nama orang
32+
func (p Person) CallMe() {
33+
fmt.Println(p.ID, p.Name)
34+
}
35+
36+
func main() {
37+
// Membuat objek Person dengan ID 123 dan nama "Jhon Pardy"
38+
person := Person{
39+
ID: 123,
40+
Name: "Jhon Pardy",
41+
}
42+
43+
// Memulai goroutine untuk mencetak pesan "Yes" sebanyak 5 kali
44+
go LoopingPrint(5, "Yes")
45+
46+
// Memulai goroutine untuk mencetak ID dan nama orang
47+
go person.CallMe()
48+
49+
// Memulai goroutine untuk mengembalikan string nama orang
50+
go func() {
51+
fmt.Println(person.SayMyName())
52+
}()
53+
54+
// Memberi waktu untuk goroutine selesai sebelum program berakhir
55+
time.Sleep(time.Second)
56+
57+
// Mencetak pesan "Done" setelah semua goroutine selesai
58+
fmt.Println("Done")
59+
}

interface/main.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// ================= INTERFACE =================
8+
9+
// Interface HasName: Digunakan untuk mendapatkan informasi seseorang
10+
// Struct yang mengimplementasikan interface ini harus memiliki kedua method ini.
11+
type HasName interface {
12+
GetPeopleInformation() string
13+
GetPeopleIdentify() string
14+
}
15+
16+
// Interface Role: Digunakan untuk mendapatkan peran seseorang
17+
type Role interface {
18+
GetRole() string
19+
}
20+
21+
// Interface EmbedInterface: Menggabungkan beberapa interface
22+
// Struct yang mengimplementasikan interface ini harus memiliki semua method dari HasName dan Role
23+
type EmbedInterface interface {
24+
ID() string
25+
HasName // Meng-embed interface HasName
26+
Role // Meng-embed interface Role
27+
}
28+
29+
// ================= STRUCT PERSON =================
30+
31+
// Struct Person mengimplementasikan interface EmbedInterface
32+
type Person struct {
33+
IDValue string // ID dari orang tersebut
34+
Name string // Nama orang
35+
Country string // Negara asal
36+
Sex bool // Jenis kelamin (true = Male, false = Female)
37+
Role string // Peran orang tersebut
38+
}
39+
40+
// ================= IMPLEMENTASI METHOD =================
41+
42+
// Implementasi method GetPeopleInformation() dari interface HasName
43+
func (p Person) GetPeopleInformation() string {
44+
return p.Name + " dari " + p.Country
45+
}
46+
47+
// Implementasi method GetPeopleIdentify() dari interface HasName
48+
func (p Person) GetPeopleIdentify() string {
49+
if p.Sex {
50+
return "Male"
51+
}
52+
return "Female"
53+
}
54+
55+
// Implementasi method GetRole() dari interface Role
56+
func (p Person) GetRole() string {
57+
return p.Role
58+
}
59+
60+
// Implementasi method ID() dari interface EmbedInterface
61+
func (p Person) ID() string {
62+
return p.IDValue
63+
}
64+
65+
// ================= FUNGSI MENGGUNAKAN INTERFACE =================
66+
67+
// Fungsi SayHello menerima parameter dengan interface EmbedInterface
68+
func SayHello(e EmbedInterface) {
69+
fmt.Println("Halo", e.GetPeopleInformation())
70+
fmt.Println("Identify as:", e.GetPeopleIdentify())
71+
fmt.Println("Role:", e.GetRole())
72+
fmt.Println("ID:", e.ID())
73+
}
74+
75+
// ================= INTERFACE KOSONG =================
76+
77+
// Interface kosong dapat menerima nilai dengan tipe data apa pun.
78+
type ExampleInterfaceEmpty interface{} // Bisa juga menggunakan `any` di Go 1.18+
79+
80+
// Fungsi untuk menampilkan nilai dari interface kosong
81+
func CallExampleInterfaceEmpty(e ExampleInterfaceEmpty) {
82+
fmt.Println("Interface Empty Value:", e)
83+
}
84+
85+
// ================= FUNGSI UTAMA (MAIN) =================
86+
87+
func main() {
88+
// Membuat objek dari struct Person
89+
person := Person{
90+
IDValue: "12345",
91+
Name: "Tom Hardy",
92+
Country: "England",
93+
Sex: true,
94+
Role: "Actor",
95+
}
96+
97+
// Memanggil fungsi SayHello() dengan objek person
98+
SayHello(person)
99+
100+
fmt.Println("\n=== Contoh Penggunaan Interface Kosong ===")
101+
102+
// Memanggil fungsi CallExampleInterfaceEmpty() dengan berbagai tipe data
103+
CallExampleInterfaceEmpty(true) // Boolean
104+
CallExampleInterfaceEmpty("John") // String
105+
CallExampleInterfaceEmpty(123) // Integer
106+
CallExampleInterfaceEmpty([]string{"Actor", "Musician", "President"}) // Slice
107+
108+
}

method/main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// Struct Student dengan Name (map), Age (int), dan Grade (slice int)
6+
type Student struct {
7+
Name map[string]string
8+
Age int
9+
Grade []int
10+
}
11+
12+
// Method untuk menampilkan data Student dengan format lebih rapi
13+
func (s Student) ShowStudents() {
14+
fmt.Printf("Name: %s %s | Age: %d | Total Grades: %d | Grades: %v\n",
15+
s.Name["firstName"], s.Name["lastName"], s.Age, len(s.Grade), s.Grade)
16+
}
17+
18+
// Method untuk update age menggunakan pointer
19+
func (s *Student) UpdateAge(newAge int) {
20+
s.Age = newAge
21+
}
22+
23+
func main() {
24+
// Membuat slice of Student
25+
students := []Student{
26+
{
27+
Name: map[string]string{"firstName": "Tom", "lastName": "Hardy"},
28+
Age: 34,
29+
Grade: []int{
30+
50, 60, 34,
31+
},
32+
},
33+
{
34+
Name: map[string]string{"firstName": "Jhon", "lastName": "Wayne"},
35+
Age: 30,
36+
Grade: []int{
37+
70, 85, 90,
38+
},
39+
},
40+
}
41+
42+
// Menampilkan semua data sebelum update
43+
fmt.Println("\n===== SEBELUM UPDATE DATA =====")
44+
for _, student := range students {
45+
student.ShowStudents()
46+
}
47+
48+
// Mengubah semua umur menjadi 34
49+
for i := range students {
50+
students[i].UpdateAge(34)
51+
}
52+
53+
// Mengubah umur hanya untuk index pertama menjadi 12
54+
students[0].UpdateAge(12)
55+
56+
// Menampilkan semua data setelah update
57+
fmt.Println("\n===== SETELAH UPDATE DATA =====")
58+
for _, student := range students {
59+
student.ShowStudents()
60+
}
61+
}

playground.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

pointer/main.go

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,54 @@ package main
22

33
import "fmt"
44

5-
/*
6-
Pointer adalah reference atau alamat memori.
7-
Variabel pointer berarti variabel yang berisi alamat memori suatu nilai.
8-
9-
Variabel biasa bisa diambil nilai pointernya, caranya dengan menambahkan tanda ampersand (&) tepat sebelum nama variabel. Metode ini disebut dengan referencing.
10-
Dan sebaliknya, nilai asli variabel pointer juga bisa diambil, dengan cara menambahkan tanda asterisk (*) tepat sebelum nama variabel. Metode ini disebut dengan dereferencing.
11-
*/
12-
13-
func main() {
14-
15-
x := "Tom Hardy"
16-
y := 021
17-
18-
var fullName *string = &x
19-
var phoneNumber *int = &y
5+
// Struct Person
6+
type Person struct {
7+
Name string
8+
Age int
9+
Country string
10+
}
2011

21-
fmt.Println(&x)
22-
fmt.Println(fullName)
12+
// Method untuk mengubah umur
13+
func (p *Person) ChangeAge(newAge int) {
14+
p.Age = newAge
15+
}
2316

24-
fmt.Println(&y)
25-
fmt.Println(phoneNumber)
17+
// Method untuk mengubah nama
18+
func (p *Person) ChangeName(newName string) {
19+
p.Name = newName
20+
}
2621

27-
x = "Jhon"
28-
fmt.Println(&x)
22+
// Method untuk mengubah negara
23+
func (p *Person) ChangeCountry(newCountry string) {
24+
p.Country = newCountry
25+
}
2926

27+
func main() {
28+
x := Person{
29+
Name: "Tom Hardy",
30+
Age: 34,
31+
Country: "England",
32+
}
33+
34+
fmt.Println("Sebelum:", x)
35+
36+
// Mengubah nilai dengan pointer method
37+
x.ChangeAge(40)
38+
x.ChangeName("Chris Hemsworth")
39+
x.ChangeCountry("Australia")
40+
fmt.Println("Sesudah:", x)
41+
42+
// Cek alamat memori
43+
y := &x
44+
fmt.Println("\nPointer y menunjuk ke x:", y)
45+
fmt.Println("Nilai yang ditunjuk y:", y.Name, y.Age, y.Country)
46+
47+
// Menampilkan alamat memori dengan &
48+
fmt.Println("\nAlamat Memori Struct x:", &x)
49+
fmt.Println("Alamat Memori Name:", &x.Name)
50+
fmt.Println("Alamat Memori Age:", &x.Age)
51+
fmt.Println("Alamat Memori Country:", &x.Country)
52+
53+
// Menampilkan nilai struct melalui pointer y
54+
fmt.Println("\nNilai Struct melalui Pointer y:", *y)
3055
}

0 commit comments

Comments
 (0)