Skip to content

Commit 31e5f79

Browse files
Afifurrohmannovalagung
andauthored
refactor: Fix deprecated io/ioutil (#243)
* refactor: Add warning when import using dot prefix * refactor: Add `env`, `dotenv`, `tfvars`, `ini`, `hcl` to list support `viper` config type * refactor: Fix deprecated `io/ioutil` change `ioutil.WriteFile` to `os.WriteFile`, `ioutil.ReadFile` to `os.ReadFile`, `ioutil.ReadAll` to `io.ReadAll` * feat: update contributors * refactor: sentences --------- Co-authored-by: novalagung <hello@novalagung.com>
1 parent ed62eaa commit 31e5f79

17 files changed

+50
-46
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,7 @@ typings/
9292

9393
# Desktop Service Store
9494
.DS_Store
95+
96+
# IDE
97+
.idea
98+
.vscode

content/A-concurrency-pipeline.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ package main
7474

7575
import (
7676
"fmt"
77-
"io/ioutil"
7877
"log"
7978
"math/rand"
8079
"os"
@@ -128,7 +127,7 @@ func generateFiles() {
128127
for i := 0; i < totalFile; i++ {
129128
filename := filepath.Join(tempPath, fmt.Sprintf("file-%d.txt", i))
130129
content := randomString(contentLength)
131-
err := ioutil.WriteFile(filename, []byte(content), os.ModePerm)
130+
err := os.WriteFile(filename, []byte(content), os.ModePerm)
132131
if err != nil {
133132
log.Println("Error writing file", filename)
134133
}
@@ -164,7 +163,6 @@ package main
164163
import (
165164
"crypto/md5"
166165
"fmt"
167-
"io/ioutil"
168166
"log"
169167
"os"
170168
"path/filepath"
@@ -209,7 +207,7 @@ func proceed() {
209207
counterTotal++
210208

211209
// read file
212-
buf, err := ioutil.ReadFile(path)
210+
buf, err := os.ReadFile(path)
213211
if err != nil {
214212
return err
215213
}
@@ -241,7 +239,7 @@ Cukup panjang isi fungsi ini, tetapi isinya cukup *straightforward* kok.
241239
* Kedua, kita siapkan `counterRenamed` sebagai counter jumlah file yang berhasil di-rename. Untuk ini juga idealnya sama dengan nilai pada `counterTotal`, kecuali ada error
242240
* Kita gunakan `filepath.Walk` untuk melakukan pembacaan semua file yang ada dalam folder `$TEMP/chapter-A.59-pipeline-temp`.
243241
* File akan dibaca secara sekuensial, di tiap pembacaan jika ada error dan ditemukan sebuah direktori, maka kita ignore kemudian lanjut pembacaan file selanjutnya.
244-
* File dibaca menggunakan `ioutil.ReadFile()`, kemudian lewat fungsi `md5.Sum()` kita cari md5 hash sum dari konten file.
242+
* File dibaca menggunakan `os.ReadFile()`, kemudian lewat fungsi `md5.Sum()` kita cari md5 hash sum dari konten file.
245243
* Setelahnya, kita rename file dengan nama `file-<md5hash>.txt`.
246244

247245
Semoga cukup jelas. Kalo iya, jalankan programnya.
@@ -274,7 +272,6 @@ package main
274272
import (
275273
"crypto/md5"
276274
"fmt"
277-
"io/ioutil"
278275
"log"
279276
"os"
280277
"path/filepath"
@@ -329,7 +326,7 @@ func readFiles() <-chan FileInfo {
329326
return nil
330327
}
331328

332-
buf, err := ioutil.ReadFile(path)
329+
buf, err := os.ReadFile(path)
333330
if err != nil {
334331
return err
335332
}

content/A-pipeline-context-cancellation.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ package main
2626

2727
import (
2828
"fmt"
29-
"io/ioutil"
3029
"log"
3130
"math/rand"
3231
"os"
@@ -147,7 +146,7 @@ func createFiles(chanIn <-chan FileInfo, numberOfWorkers int) <-chan FileInfo {
147146
for job := range chanIn {
148147
filePath := filepath.Join(tempPath, job.FileName)
149148
content := randomString(contentLength)
150-
err := ioutil.WriteFile(filePath, []byte(content), os.ModePerm)
149+
err := os.WriteFile(filePath, []byte(content), os.ModePerm)
151150

152151
log.Println("worker", workerIndex, "working on", job.FileName, "file generation")
153152

@@ -381,7 +380,7 @@ func createFiles(ctx context.Context, chanIn <-chan FileInfo, numberOfWorkers in
381380
default:
382381
filePath := filepath.Join(tempPath, job.FileName)
383382
content := randomString(contentLength)
384-
err := ioutil.WriteFile(filePath, []byte(content), os.ModePerm)
383+
err := os.WriteFile(filePath, []byte(content), os.ModePerm)
385384

386385
log.Println("worker", workerIndex, "working on", job.FileName, "file generation")
387386

content/A-properti-public-dan-private.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,11 @@ Dari contoh program di atas, bisa disimpulkan bahwa untuk menggunakan `struct` y
201201

202202
## A.26.5. Import Dengan Prefix Tanda Titik
203203

204+
> PERINGATAN! Penggunaan tanda titik pada saat import package bisa menyebabkan kode menjadi ambigu, karena alasan tersebut teknik import ini kurang direkomendasikan.
205+
204206
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()`.
205207

206208
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.
207-
208209
```go
209210
import (
210211
. "belajar-golang-level-akses/library"

content/A-simplified-fan-in-fan-out-pipeline.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ package main
2727

2828
import (
2929
"fmt"
30-
"io/ioutil"
3130
"log"
3231
"math/rand"
3332
"os"
@@ -82,7 +81,7 @@ func generateFiles() {
8281
for i := 0; i < totalFile; i++ {
8382
filename := filepath.Join(tempPath, fmt.Sprintf("file-%d.txt", i))
8483
content := randomString(contentLength)
85-
err := ioutil.WriteFile(filename, []byte(content), os.ModePerm)
84+
err := os.WriteFile(filename, []byte(content), os.ModePerm)
8685
if err != nil {
8786
log.Println("Error writing file", filename)
8887
}
@@ -113,7 +112,6 @@ package main
113112

114113
import (
115114
"fmt"
116-
"io/ioutil"
117115
"log"
118116
"math/rand"
119117
"os"
@@ -269,7 +267,7 @@ func createFiles(chanIn <-chan FileInfo, numberOfWorkers int) <-chan FileInfo {
269267
// do the jobs
270268
filePath := filepath.Join(tempPath, job.FileName)
271269
content := randomString(contentLength)
272-
err := ioutil.WriteFile(filePath, []byte(content), os.ModePerm)
270+
err := os.WriteFile(filePath, []byte(content), os.ModePerm)
273271

274272
log.Println("worker", workerIndex, "working on", job.FileName, "file generation")
275273

content/B-server-handler-http-request-cancellation.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"net/http"
2727
"strings"
2828
"time"
29+
"log"
2930
)
3031

3132
func handleIndex(w http.ResponseWriter, r *http.Request) {
@@ -97,7 +98,7 @@ go func() {
9798
// do the process here
9899
// simulate a long-time request by putting 10 seconds sleep
99100

100-
body, err := ioutil.ReadAll(r.Body)
101+
body, err := io.ReadAll(r.Body)
101102
// ...
102103

103104
time.Sleep(10 * time.Second)

content/B-simple-configuration.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ package conf
5151

5252
import (
5353
"encoding/json"
54-
"io/ioutil"
5554
"os"
5655
"path/filepath"
5756
"time"
@@ -93,7 +92,7 @@ func init() {
9392
return
9493
}
9594

96-
bts, err := ioutil.ReadFile(filepath.Join(basePath, "conf", "config.json"))
95+
bts, err := os.ReadFile(filepath.Join(basePath, "conf", "config.json"))
9796
if err != nil {
9897
panic(err)
9998
return

content/C-advanced-configuration-viper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ Berikut merupakan list format yang didukung oleh viper.
5858
- properties
5959
- props
6060
- prop
61+
- env
62+
- dotenv
63+
- tfvars
64+
- ini
65+
- hcl
6166

6267
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.
6368

content/C-golang-ftp.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ if err != nil {
157157
log.Fatal(err.Error())
158158
}
159159

160-
test1ContentInBytes, err := ioutil.ReadAll(fileTest1)
160+
test1ContentInBytes, err := io.ReadAll(fileTest1)
161161
fileTest1.Close()
162162
if err != nil {
163163
log.Fatal(err.Error())
@@ -166,9 +166,9 @@ if err != nil {
166166
fmt.Println(" ->", fileTest1Path, "->", string(test1ContentInBytes))
167167
```
168168

169-
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).
169+
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).
170170

171-
> Jangan lupa untuk import package `io/ioutil`.
171+
> Jangan lupa untuk import package `io`.
172172
173173
Di kode di atas file `test1.txt` dibaca. Lakukan operasi yang sama pada file `somefolder/test3.txt`.
174174

@@ -179,7 +179,7 @@ if err != nil {
179179
log.Fatal(err.Error())
180180
}
181181

182-
test2ContentInBytes, err := ioutil.ReadAll(fileTest2)
182+
test2ContentInBytes, err := io.ReadAll(fileTest2)
183183
fileTest2.Close()
184184
if err != nil {
185185
log.Fatal(err.Error())

content/C-golang-jwt.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ import (
120120
"context"
121121
"encoding/json"
122122
"fmt"
123-
"io/ioutil"
124123
"net/http"
125124
"os"
126125
"path/filepath"
@@ -261,7 +260,7 @@ Bagian otentikasi dan generate token sebenarnya cukup sampai di sini. Tapi seben
261260
func authenticateUser(username, password string) (bool, M) {
262261
basePath, _ := os.Getwd()
263262
dbPath := filepath.Join(basePath, "users.json")
264-
buf, _ := ioutil.ReadFile(dbPath)
263+
buf, _ := os.ReadFile(dbPath)
265264

266265
data := make([]M, 0)
267266
err := json.Unmarshal(buf, &data)

0 commit comments

Comments
 (0)