11 Jan 2023
Owner, Group, Other 對檔案/資料夾的 write, read, execute 三個動作分別具有不同的權限。
權限通常有兩種表示方法,第一種方式是用三個 octal number 作為權限的表示,第一個數字是 read 權限,第二個數字是 write 權限,第三個數字是 execute 權限;第二種方法是直接用 rwx 來表示。以下舉例:
- 0 or —: No permissions
- 1 or –x: Execute permission (execute a file or navigate to a directory)
- 2 or -w-: Write permission (write a file or add new files in a directory)
- 3 or -wx: Write and execute
- 4 or r–: Read permission (read a file or list the contents of a directory)
- 5 or r-x: Read and execute
- 6 or rw-: Read and write
- 7 or rwx: Read, write, and execute
Owner, Group, Other 對一個檔案的權限,就用三個 octal number 或三組 rwx 表示:
- 777: Everyone can read, write, and execute.
- 700: Owner can read, write, and execute.
- 664: Owner and group can read and write.
- 640: Owner can read and write and group can read.
- 755: Owner can read, write, and execute, while group and others can read and execute.
可以用 ls -l
或 ll
來瀏覽目前目錄底下檔案/資料夾的權限設置。
會發現下面除了三組 rwx,有些檔案前面還有一個 d,這種 1333 的形式分別代表:檔案屬性/ owner 擁有者權限 / group 同群組使用者權限 / other 其他人權限。而 d 代表該檔案是個目錄。
有些檔案除了 1333,後面還有一個 @,這代表該檔案是 extended attributes。
30 Dec 2022
Variadic Functions,可變參數函數,讓函數參數數量可是未定義,可能是一個或多個,也可能是零個。
fmt.Println
就是 Go 常見的 Variadic Functions。
以加總函數 sum 為例,可以有下面兩種寫法:
package main
import "fmt"
// Variadic Functions
func sum(nums ...int) {
fmt.Println("nums: ", nums)
total := 0
for _, n := range nums {
total += n
}
fmt.Println("sum: ", total)
}
// 傳入陣列
func sum2(nums []int) {
fmt.Println("nums: ", nums)
total := 0
for _, n := range nums {
total += n
}
fmt.Println("sum: ", total)
}
func main() {
arr := []int{1, 3, 5, 7, 9}
sum(arr...)
sum2(arr)
}
28 Dec 2022
建立資料夾、進入資料夾、初始化
mkdir hello
cd hello
go mod init example
建立放 proto
檔案的資料夾、建立 .proto
mkdir person
cd person
touch person.proto
在person.proto
貼上以下程式碼
syntax = "proto3"; // 3 代表版本
option go_package = "example/person";
package person;
message Person {
string name = 1; // 有型別,和JSON不同,所以編譯器才能自動產生很多不同語言的版本
}
執行:
protoc *.proto --go_out=plugins=grpc:. --go_opt=paths=source_relative
回到根目錄並建立 main.go
在main.go
貼上以下程式碼
package main
import (
"fmt"
"io/ioutil"
"log"
pb "example/person"
proto "github.com/golang/protobuf/proto"
)
func main() {
person := &pb.Person{Name: "XXX"}
fmt.Printf("Person's name is %s\n", person.GetName())
//把 person object 寫到檔案 person.bin(人類不可讀)
out, err := proto.Marshal(person)
if err != nil {
log.Fatalf("Serialization error: %s", err.Error())
}
if err := ioutil.WriteFile("person.bin", out, 0644); err != nil {
log.Fatalf("Write File Error: %s ", err.Error())
}
fmt.Println("Write Success")
//讀檔然後印出來
in, err := ioutil.ReadFile("person.bin")
if err != nil {
log.Fatalf("Read File Error: %s ", err.Error())
}
person2 := &pb.Person{}
err2 := proto.Unmarshal(in, person2)
if err2 != nil {
log.Fatalf("DeSerialization error: %s", err.Error())
}
fmt.Println("Read Success")
fmt.Printf("Person2's name is %s\n", person2.GetName())
}
下載需要的 package
go get -u github.com/golang/protobuf/proto
執行程式
參考資料
Protocol Buffers and Go: Getting started
14 Dec 2022
Go 簡單的加解密可以用 base64 來實現
import (
"encoding/base64"
"fmt"
)
func main() {
data := "你好世界"
sEnc := base64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(sEnc)
sDec, err := base64.StdEncoding.DecodeString(sEnc)
if err == nil {
fmt.Println(string(sDec))
}
}
其他還有例如 DES、AES,這兩個演算法在 crypto
package 內被實現。AES 的範例如下:
import (
"crypto/aes"
"crypto/cipher"
"fmt"
"os"
)
var commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
func main() {
//需要去加密的字串
plaintext := []byte("你好世界")
//如果傳入加密串的話,plaintext 就是傳入的字串
if len(os.Args) > 1 {
plaintext = []byte(os.Args[1])
}
//aes 的加密字串
key_text := "astaxie12798akljzmknm.ahkjkljl;k"
if len(os.Args) > 2 {
key_text = os.Args[2]
}
// 建立加密演算法 aes
c, err := aes.NewCipher([]byte(key_text))
if err != nil {
fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
os.Exit(-1)
}
//加密字串
cfb := cipher.NewCFBEncrypter(c, commonIV)
ciphertext := make([]byte, len(plaintext))
cfb.XORKeyStream(ciphertext, plaintext)
fmt.Printf("%s=>%x\n", plaintext, ciphertext)
// 解密字串
cfbdec := cipher.NewCFBDecrypter(c, commonIV)
plaintextCopy := make([]byte, len(plaintext))
cfbdec.XORKeyStream(plaintextCopy, ciphertext)
fmt.Printf("%x=>%s\n", ciphertext, plaintextCopy)
}
參考資料
程式碼來源