Skip to content

Commit

Permalink
refactor: Fix deprecated io/ioutil (#243)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Afifurrohman and novalagung authored Nov 8, 2023
1 parent ed62eaa commit 31e5f79
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 46 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ typings/

# Desktop Service Store
.DS_Store

# IDE
.idea
.vscode
11 changes: 4 additions & 7 deletions content/A-concurrency-pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -164,7 +163,6 @@ package main
import (
"crypto/md5"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -209,7 +207,7 @@ func proceed() {
counterTotal++

// read file
buf, err := ioutil.ReadFile(path)
buf, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -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-<md5hash>.txt`.

Semoga cukup jelas. Kalo iya, jalankan programnya.
Expand Down Expand Up @@ -274,7 +272,6 @@ package main
import (
"crypto/md5"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions content/A-pipeline-context-cancellation.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down
3 changes: 2 additions & 1 deletion content/A-properti-public-dan-private.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,11 @@ Dari contoh program di atas, bisa disimpulkan bahwa untuk menggunakan `struct` y

## A.26.5. Import Dengan Prefix Tanda Titik

> 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()`.

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"
Expand Down
6 changes: 2 additions & 4 deletions content/A-simplified-fan-in-fan-out-pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -113,7 +112,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -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")

Expand Down
3 changes: 2 additions & 1 deletion content/B-server-handler-http-request-cancellation.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/http"
"strings"
"time"
"log"
)

func handleIndex(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions content/B-simple-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ package conf

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions content/C-advanced-configuration-viper.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions content/C-golang-ftp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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`.

Expand All @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions content/C-golang-jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions content/C-golang-ssh-sftp.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package main

import (
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"os"
)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion content/C-secure-insecure-client-http-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions content/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions content/D-golang-web-socket-chatting-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"fmt"
"github.com/gorilla/websocket"
gubrak "github.com/novalagung/gubrak/v2"
"io/ioutil"
"os"
"log"
"net/http"
"strings"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion content/D-google-api-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ package main

import (
"context"
"io"
"fmt"
"errors"
"time"
"net/http"
)

Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions etc/fix-ebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -61,7 +60,7 @@ func preAdjustment() {
return nil
}

buf, err := ioutil.ReadFile(path)
buf, err := os.ReadFile(path)
if err != nil {
return err
}
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 31e5f79

Please sign in to comment.