From 193c4872cb468b67738735af1925e26fd2adb55d Mon Sep 17 00:00:00 2001 From: afifurrohman-id Date: Sat, 4 Nov 2023 09:07:43 +0700 Subject: [PATCH 1/5] refactor: Add warning when import using dot prefix --- .gitignore | 4 ++++ content/A-properti-public-dan-private.md | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 42a972ec6..670df4627 100644 --- a/.gitignore +++ b/.gitignore @@ -92,3 +92,7 @@ typings/ # Desktop Service Store .DS_Store + +# IDE +.idea +.vscode \ No newline at end of file diff --git a/content/A-properti-public-dan-private.md b/content/A-properti-public-dan-private.md index 53eb16a26..f15496e4a 100644 --- a/content/A-properti-public-dan-private.md +++ b/content/A-properti-public-dan-private.md @@ -200,11 +200,11 @@ Dari contoh program di atas, bisa disimpulkan bahwa untuk menggunakan `struct` y ![Contoh penerapan pemanfaatan struct dan propertynya dari package berbeda](images/A_properti_public_private_4_success.png) ## A.26.5. Import Dengan Prefix Tanda Titik +> PERINGATAN! Menggunakan tanda titik pada saat import package bisa menyebabkan kode menjadi ambigu. Oleh karena itu, penggunaan tanda titik pada saat import package tidak disarankan untuk golang versi terbaru. Seperti yang kita tahu, untuk mengakses fungsi/struct/variabel yg berada di package lain, nama package nya perlu ditulis, contohnya seperti pada penggunaan `library.Student` dan `fmt.Println()`. Di Go, komponen yang berada di package lain yang di-import bisa dijadikan se-level dengan komponen package peng-import, caranya dengan menambahkan tanda titik (`.`) setelah penulisan keyword `import`. Maksud dari se-level di sini adalah, semua properti di package lain yg di-import bisa diakses tanpa perlu menuliskan nama package, seperti ketika mengakses sesuatu dari file yang sama. - ```go import ( . "belajar-golang-level-akses/library" From 568a9c294e41129478fcd7584a5f21508c52e047 Mon Sep 17 00:00:00 2001 From: afifurrohman-id Date: Sat, 4 Nov 2023 09:21:59 +0700 Subject: [PATCH 2/5] refactor: Add `env`, `dotenv`, `tfvars`, `ini`, `hcl` to list support `viper` config type --- content/C-advanced-configuration-viper.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/content/C-advanced-configuration-viper.md b/content/C-advanced-configuration-viper.md index caca9074e..ba24c88c1 100644 --- a/content/C-advanced-configuration-viper.md +++ b/content/C-advanced-configuration-viper.md @@ -58,6 +58,11 @@ Berikut merupakan list format yang didukung oleh viper. - properties - props - prop + - env + - dotenv + - tfvars + - ini + - hcl Fungsi `.AddConfigPath()` digunakan untuk mendaftarkan path folder di mana file-file konfigurasi berada. Fungsi ini bisa dipanggil beberapa kali, jika memang ada banyak file konfigurasi tersimpan dalam path berbeda. From 294b2f83ff678de1b6c510c8c8c8604500465535 Mon Sep 17 00:00:00 2001 From: afifurrohman-id Date: Sat, 4 Nov 2023 10:17:27 +0700 Subject: [PATCH 3/5] refactor: Fix deprecated `io/ioutil` change `ioutil.WriteFile` to `os.WriteFile`, `ioutil.ReadFile` to `os.ReadFile`, `ioutil.ReadAll` to `io.ReadAll` --- content/A-concurrency-pipeline.md | 11 ++++------ content/A-pipeline-context-cancellation.md | 5 ++--- .../A-simplified-fan-in-fan-out-pipeline.md | 6 ++---- ...erver-handler-http-request-cancellation.md | 3 ++- content/B-simple-configuration.md | 3 +-- content/C-golang-ftp.md | 8 +++---- content/C-golang-jwt.md | 3 +-- content/C-golang-ssh-sftp.md | 3 +-- .../C-secure-insecure-client-http-request.md | 2 +- content/D-golang-web-socket-chatting-app.md | 4 ++-- content/D-google-api-search.md | 5 ++++- etc/fix-ebook.go | 9 ++++---- etc/fix-webbook.go | 21 +++++++++---------- 13 files changed, 38 insertions(+), 45 deletions(-) diff --git a/content/A-concurrency-pipeline.md b/content/A-concurrency-pipeline.md index 65b50177b..7300a989d 100644 --- a/content/A-concurrency-pipeline.md +++ b/content/A-concurrency-pipeline.md @@ -74,7 +74,6 @@ package main import ( "fmt" - "io/ioutil" "log" "math/rand" "os" @@ -128,7 +127,7 @@ func generateFiles() { for i := 0; i < totalFile; i++ { filename := filepath.Join(tempPath, fmt.Sprintf("file-%d.txt", i)) content := randomString(contentLength) - err := ioutil.WriteFile(filename, []byte(content), os.ModePerm) + err := os.WriteFile(filename, []byte(content), os.ModePerm) if err != nil { log.Println("Error writing file", filename) } @@ -164,7 +163,6 @@ package main import ( "crypto/md5" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -209,7 +207,7 @@ func proceed() { counterTotal++ // read file - buf, err := ioutil.ReadFile(path) + buf, err := os.ReadFile(path) if err != nil { return err } @@ -241,7 +239,7 @@ Cukup panjang isi fungsi ini, tetapi isinya cukup *straightforward* kok. * Kedua, kita siapkan `counterRenamed` sebagai counter jumlah file yang berhasil di-rename. Untuk ini juga idealnya sama dengan nilai pada `counterTotal`, kecuali ada error * Kita gunakan `filepath.Walk` untuk melakukan pembacaan semua file yang ada dalam folder `$TEMP/chapter-A.59-pipeline-temp`. * File akan dibaca secara sekuensial, di tiap pembacaan jika ada error dan ditemukan sebuah direktori, maka kita ignore kemudian lanjut pembacaan file selanjutnya. -* File dibaca menggunakan `ioutil.ReadFile()`, kemudian lewat fungsi `md5.Sum()` kita cari md5 hash sum dari konten file. +* File dibaca menggunakan `os.ReadFile()`, kemudian lewat fungsi `md5.Sum()` kita cari md5 hash sum dari konten file. * Setelahnya, kita rename file dengan nama `file-.txt`. Semoga cukup jelas. Kalo iya, jalankan programnya. @@ -274,7 +272,6 @@ package main import ( "crypto/md5" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -329,7 +326,7 @@ func readFiles() <-chan FileInfo { return nil } - buf, err := ioutil.ReadFile(path) + buf, err := os.ReadFile(path) if err != nil { return err } diff --git a/content/A-pipeline-context-cancellation.md b/content/A-pipeline-context-cancellation.md index b0692bf80..d61c5a459 100644 --- a/content/A-pipeline-context-cancellation.md +++ b/content/A-pipeline-context-cancellation.md @@ -26,7 +26,6 @@ package main import ( "fmt" - "io/ioutil" "log" "math/rand" "os" @@ -147,7 +146,7 @@ func createFiles(chanIn <-chan FileInfo, numberOfWorkers int) <-chan FileInfo { for job := range chanIn { filePath := filepath.Join(tempPath, job.FileName) content := randomString(contentLength) - err := ioutil.WriteFile(filePath, []byte(content), os.ModePerm) + err := os.WriteFile(filePath, []byte(content), os.ModePerm) log.Println("worker", workerIndex, "working on", job.FileName, "file generation") @@ -381,7 +380,7 @@ func createFiles(ctx context.Context, chanIn <-chan FileInfo, numberOfWorkers in default: filePath := filepath.Join(tempPath, job.FileName) content := randomString(contentLength) - err := ioutil.WriteFile(filePath, []byte(content), os.ModePerm) + err := os.WriteFile(filePath, []byte(content), os.ModePerm) log.Println("worker", workerIndex, "working on", job.FileName, "file generation") diff --git a/content/A-simplified-fan-in-fan-out-pipeline.md b/content/A-simplified-fan-in-fan-out-pipeline.md index 9dc5493c1..bc8042795 100644 --- a/content/A-simplified-fan-in-fan-out-pipeline.md +++ b/content/A-simplified-fan-in-fan-out-pipeline.md @@ -27,7 +27,6 @@ package main import ( "fmt" - "io/ioutil" "log" "math/rand" "os" @@ -82,7 +81,7 @@ func generateFiles() { for i := 0; i < totalFile; i++ { filename := filepath.Join(tempPath, fmt.Sprintf("file-%d.txt", i)) content := randomString(contentLength) - err := ioutil.WriteFile(filename, []byte(content), os.ModePerm) + err := os.WriteFile(filename, []byte(content), os.ModePerm) if err != nil { log.Println("Error writing file", filename) } @@ -113,7 +112,6 @@ package main import ( "fmt" - "io/ioutil" "log" "math/rand" "os" @@ -269,7 +267,7 @@ func createFiles(chanIn <-chan FileInfo, numberOfWorkers int) <-chan FileInfo { // do the jobs filePath := filepath.Join(tempPath, job.FileName) content := randomString(contentLength) - err := ioutil.WriteFile(filePath, []byte(content), os.ModePerm) + err := os.WriteFile(filePath, []byte(content), os.ModePerm) log.Println("worker", workerIndex, "working on", job.FileName, "file generation") diff --git a/content/B-server-handler-http-request-cancellation.md b/content/B-server-handler-http-request-cancellation.md index e716419ca..e115c5855 100644 --- a/content/B-server-handler-http-request-cancellation.md +++ b/content/B-server-handler-http-request-cancellation.md @@ -26,6 +26,7 @@ import ( "net/http" "strings" "time" + "log" ) func handleIndex(w http.ResponseWriter, r *http.Request) { @@ -97,7 +98,7 @@ go func() { // do the process here // simulate a long-time request by putting 10 seconds sleep - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) // ... time.Sleep(10 * time.Second) diff --git a/content/B-simple-configuration.md b/content/B-simple-configuration.md index eafb9efd0..d4c3aad76 100644 --- a/content/B-simple-configuration.md +++ b/content/B-simple-configuration.md @@ -51,7 +51,6 @@ package conf import ( "encoding/json" - "io/ioutil" "os" "path/filepath" "time" @@ -93,7 +92,7 @@ func init() { return } - bts, err := ioutil.ReadFile(filepath.Join(basePath, "conf", "config.json")) + bts, err := os.ReadFile(filepath.Join(basePath, "conf", "config.json")) if err != nil { panic(err) return diff --git a/content/C-golang-ftp.md b/content/C-golang-ftp.md index e0ce2176e..001d7e657 100644 --- a/content/C-golang-ftp.md +++ b/content/C-golang-ftp.md @@ -157,7 +157,7 @@ if err != nil { log.Fatal(err.Error()) } -test1ContentInBytes, err := ioutil.ReadAll(fileTest1) +test1ContentInBytes, err := io.ReadAll(fileTest1) fileTest1.Close() if err != nil { log.Fatal(err.Error()) @@ -166,9 +166,9 @@ if err != nil { fmt.Println(" ->", fileTest1Path, "->", string(test1ContentInBytes)) ``` -Baca isi objek response tersebut menggunakan method `.Read()` miliknya, atau bisa juga menggunakan `ioutil.ReadAll()` lebih praktisnya (nilai baliknya bertipe `[]byte` maka cast ke tipe `string` terlebih dahulu untuk menampilkan isinya). +Baca isi objek response tersebut menggunakan method `.Read()` miliknya, atau bisa juga menggunakan `io.ReadAll()` lebih praktisnya (nilai baliknya bertipe `[]byte` maka cast ke tipe `string` terlebih dahulu untuk menampilkan isinya). -> Jangan lupa untuk import package `io/ioutil`. +> Jangan lupa untuk import package `io`. Di kode di atas file `test1.txt` dibaca. Lakukan operasi yang sama pada file `somefolder/test3.txt`. @@ -179,7 +179,7 @@ if err != nil { log.Fatal(err.Error()) } -test2ContentInBytes, err := ioutil.ReadAll(fileTest2) +test2ContentInBytes, err := io.ReadAll(fileTest2) fileTest2.Close() if err != nil { log.Fatal(err.Error()) diff --git a/content/C-golang-jwt.md b/content/C-golang-jwt.md index 1159446dc..e58c6114f 100644 --- a/content/C-golang-jwt.md +++ b/content/C-golang-jwt.md @@ -120,7 +120,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "net/http" "os" "path/filepath" @@ -261,7 +260,7 @@ Bagian otentikasi dan generate token sebenarnya cukup sampai di sini. Tapi seben func authenticateUser(username, password string) (bool, M) { basePath, _ := os.Getwd() dbPath := filepath.Join(basePath, "users.json") - buf, _ := ioutil.ReadFile(dbPath) + buf, _ := os.ReadFile(dbPath) data := make([]M, 0) err := json.Unmarshal(buf, &data) diff --git a/content/C-golang-ssh-sftp.md b/content/C-golang-ssh-sftp.md index 8b840fb9a..00ffbeeee 100644 --- a/content/C-golang-ssh-sftp.md +++ b/content/C-golang-ssh-sftp.md @@ -15,7 +15,6 @@ package main import ( "golang.org/x/crypto/ssh" - "io/ioutil" "log" "os" ) @@ -57,7 +56,7 @@ sshConfig := &ssh.ClientConfig{ } func PublicKeyFile(file string) ssh.AuthMethod { - buffer, err := ioutil.ReadFile(file) + buffer, err := os.ReadFile(file) if err != nil { return nil } diff --git a/content/C-secure-insecure-client-http-request.md b/content/C-secure-insecure-client-http-request.md index 3f7d18249..e071fb0cf 100644 --- a/content/C-secure-insecure-client-http-request.md +++ b/content/C-secure-insecure-client-http-request.md @@ -109,7 +109,7 @@ Isi `client.Transport` dengan konfigurasi secure request. Hapus saja konfigurasi Kurang lebih kode-nya seperti berikut. ```go -certFile, err := ioutil.ReadFile("server.crt") +certFile, err := os.ReadFile("server.crt") if err != nil { return nil, err } diff --git a/content/D-golang-web-socket-chatting-app.md b/content/D-golang-web-socket-chatting-app.md index 361c66931..1d46d9416 100644 --- a/content/D-golang-web-socket-chatting-app.md +++ b/content/D-golang-web-socket-chatting-app.md @@ -34,7 +34,7 @@ import ( "fmt" "github.com/gorilla/websocket" gubrak "github.com/novalagung/gubrak/v2" - "io/ioutil" + "os" "log" "net/http" "strings" @@ -91,7 +91,7 @@ Selanjutnya buat fungsi `main()`, siapkan satu buah rute, `/`, isinya menampilka ```go func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - content, err := ioutil.ReadFile("index.html") + content, err := os.ReadFile("index.html") if err != nil { http.Error(w, "Could not open requested file", http.StatusInternalServerError) return diff --git a/content/D-google-api-search.md b/content/D-google-api-search.md index 32390e233..7c1beed8a 100644 --- a/content/D-google-api-search.md +++ b/content/D-google-api-search.md @@ -56,7 +56,10 @@ package main import ( "context" + "io" "fmt" + "errors" + "time" "net/http" ) @@ -242,7 +245,7 @@ go func() { if resp != nil { defer resp.Body.Close() - resData, err := ioutil.ReadAll(resp.Body) + resData, err := io.ReadAll(resp.Body) if err != nil { innerChanErr <- err return diff --git a/etc/fix-ebook.go b/etc/fix-ebook.go index 0ec89a241..ac9bd6c1b 100644 --- a/etc/fix-ebook.go +++ b/etc/fix-ebook.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -34,7 +33,7 @@ func preAdjustment() { basePath, _ := os.Getwd() readmePath := filepath.Join(basePath, "README.md") - buf, err := ioutil.ReadFile(readmePath) + buf, err := os.ReadFile(readmePath) if err != nil { log.Fatal(err.Error()) } @@ -44,7 +43,7 @@ func preAdjustment() { versionToFind := `((VERSION))` mdString = strings.ReplaceAll(mdString, versionToFind, getVersion()) - err = ioutil.WriteFile(readmePath, []byte(mdString), 0644) + err = os.WriteFile(readmePath, []byte(mdString), 0644) if err != nil { log.Fatal(err.Error()) } @@ -61,7 +60,7 @@ func preAdjustment() { return nil } - buf, err := ioutil.ReadFile(path) + buf, err := os.ReadFile(path) if err != nil { return err } @@ -72,7 +71,7 @@ func preAdjustment() { htmlString = strings.ReplaceAll(htmlString, substackEmbedToRemove, "") // ==== update file - err = ioutil.WriteFile(path, []byte(strings.TrimSpace(htmlString)), info.Mode()) + err = os.WriteFile(path, []byte(strings.TrimSpace(htmlString)), info.Mode()) if err != nil { return err } diff --git a/etc/fix-webbook.go b/etc/fix-webbook.go index a5081b8eb..2a306602b 100644 --- a/etc/fix-webbook.go +++ b/etc/fix-webbook.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -37,7 +36,7 @@ func preAdjustment() { basePath, _ := os.Getwd() readmePath := filepath.Join(basePath, "README.md") - buf, err := ioutil.ReadFile(readmePath) + buf, err := os.ReadFile(readmePath) if err != nil { log.Fatal(err.Error()) } @@ -47,7 +46,7 @@ func preAdjustment() { versionToFind := `((VERSION))` mdString = strings.ReplaceAll(mdString, versionToFind, getVersion()) - err = ioutil.WriteFile(readmePath, []byte(mdString), 0644) + err = os.WriteFile(readmePath, []byte(mdString), 0644) if err != nil { log.Fatal(err.Error()) } @@ -68,7 +67,7 @@ func postAdjustment() { return nil } - buf, err := ioutil.ReadFile(path) + buf, err := os.ReadFile(path) if err != nil { return err } @@ -131,8 +130,8 @@ func postAdjustment() { htmlString = strings.ReplaceAll(htmlString, imagesAltToFind, imagesAltReplacement) // ==== disqus lazy load - disqusJSBuf, _ := ioutil.ReadFile("./gitbook-plugin-disqus.js") - ioutil.WriteFile("./_book/gitbook/gitbook-plugin-disqus/plugin.js", disqusJSBuf, 0644) + disqusJSBuf, _ := os.ReadFile("./gitbook-plugin-disqus.js") + os.WriteFile("./_book/gitbook/gitbook-plugin-disqus/plugin.js", disqusJSBuf, 0644) // ==== gitbook assets lazy load cssToLoad := []string{ @@ -154,8 +153,8 @@ func postAdjustment() { htmlString = strings.ReplaceAll(htmlString, buttonToFind, buttonReplacement) // ==== inject adjustment css - adjustmentCSSBuf, _ := ioutil.ReadFile("./custom.css") - ioutil.WriteFile("./_book/gitbook/custom.css", adjustmentCSSBuf, 0644) + adjustmentCSSBuf, _ := os.ReadFile("./custom.css") + os.WriteFile("./_book/gitbook/custom.css", adjustmentCSSBuf, 0644) adjustmentCSSToFind := `` adjustmentCSSReplacement := `` + adjustmentCSSToFind htmlString = strings.ReplaceAll(htmlString, adjustmentCSSToFind, adjustmentCSSReplacement) @@ -192,7 +191,7 @@ func postAdjustment() { // htmlString = strings.Replace(htmlString, infoBannerToFind, infoBannerReplacement) // ==== update file - err = ioutil.WriteFile(path, []byte(strings.TrimSpace(htmlString)), info.Mode()) + err = os.WriteFile(path, []byte(strings.TrimSpace(htmlString)), info.Mode()) if err != nil { return err } @@ -206,7 +205,7 @@ func postAdjustment() { } siteMapPath := filepath.Join(basePath, "_book", "sitemap.xml") - buf, err := ioutil.ReadFile(siteMapPath) + buf, err := os.ReadFile(siteMapPath) if err != nil { log.Fatal(err.Error()) } @@ -234,7 +233,7 @@ func postAdjustment() { `)) - err = ioutil.WriteFile(siteMapPath, []byte(sitemapContent), 0644) + err = os.WriteFile(siteMapPath, []byte(sitemapContent), 0644) if err != nil { log.Fatal(err.Error()) } From 4d014b18cffe7505b296e9d85c881c8949a4ab64 Mon Sep 17 00:00:00 2001 From: novalagung Date: Wed, 8 Nov 2023 14:49:48 +0700 Subject: [PATCH 4/5] feat: update contributors --- content/CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/CONTRIBUTING.md b/content/CONTRIBUTING.md index c20782465..7a2eb0008 100644 --- a/content/CONTRIBUTING.md +++ b/content/CONTRIBUTING.md @@ -26,6 +26,7 @@ Berikut merupakan *hall of fame* kontributor yang sudah berbaik hati menyisihkan 1. [Acep Saepudin](https://github.com/acepsaepudin) 1. [Adev Saputra](https://github.com/adev22) +1. [Afifurrohman](afifurrohman-id) 1. [Agus Budiono](https://github.com/dyon048) 1. [Ahmad Syafiq Aqil Wafi](https://github.com/Syafiqjos) 1. [Akul Nurislamimanudin](https://github.com/akulnurislam) From f813ead6019842af76c7a3974bf720cae0c92ab4 Mon Sep 17 00:00:00 2001 From: novalagung Date: Wed, 8 Nov 2023 14:54:29 +0700 Subject: [PATCH 5/5] refactor: sentences --- content/A-properti-public-dan-private.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/A-properti-public-dan-private.md b/content/A-properti-public-dan-private.md index f15496e4a..aec5e0708 100644 --- a/content/A-properti-public-dan-private.md +++ b/content/A-properti-public-dan-private.md @@ -200,7 +200,8 @@ Dari contoh program di atas, bisa disimpulkan bahwa untuk menggunakan `struct` y ![Contoh penerapan pemanfaatan struct dan propertynya dari package berbeda](images/A_properti_public_private_4_success.png) ## A.26.5. Import Dengan Prefix Tanda Titik -> PERINGATAN! Menggunakan tanda titik pada saat import package bisa menyebabkan kode menjadi ambigu. Oleh karena itu, penggunaan tanda titik pada saat import package tidak disarankan untuk golang versi terbaru. + +> PERINGATAN! Penggunaan tanda titik pada saat import package bisa menyebabkan kode menjadi ambigu, karena alasan tersebut teknik import ini kurang direkomendasikan. Seperti yang kita tahu, untuk mengakses fungsi/struct/variabel yg berada di package lain, nama package nya perlu ditulis, contohnya seperti pada penggunaan `library.Student` dan `fmt.Println()`.