更多個人筆記見:
github個人筆記倉庫
gitee 個人筆記倉庫
個人學習,學習過程中還會不斷補充~ (后續會更新在github上)
文章目錄
- 進程信息OS操作
- 基本例子
- 編碼相關
- HASH 哈希
- Base64 encoding 基礎64編碼
- 數據格式轉換和處理
- 字符串和int之間
- URLparsing URL解析
進程信息OS操作
基本例子
package mainimport ("fmt""os""os/exec""runtime"
)func main() {// 1. 獲取當前進程信息fmt.Println("--- 進程信息 ---")fmt.Println("進程ID:", os.Getpid())fmt.Println("父進程ID:", os.Getppid())fmt.Println("用戶ID:", os.Getuid())fmt.Println("組ID:", os.Getgid())// 2. 獲取系統信息fmt.Println("\n--- 系統信息 ---")fmt.Println("操作系統:", runtime.GOOS)fmt.Println("CPU核心數:", runtime.NumCPU())hostname, _ := os.Hostname()fmt.Println("主機名:", hostname)// 3. 環境變量操作fmt.Println("\n--- 環境變量 ---")fmt.Println("PATH:", os.Getenv("PATH"))os.Setenv("TEST_ENV", "test_value")fmt.Println("TEST_ENV:", os.Getenv("TEST_ENV"))// 4. 執行系統命令fmt.Println("\n--- 執行命令 ---")cmd := exec.Command("echo", "Hello, Go!")output, _ := cmd.Output()fmt.Printf("命令輸出: %s", output)// 5. 文件系統操作fmt.Println("\n--- 文件操作 ---")_, err := os.Stat("test.txt")if os.IsNotExist(err) {fmt.Println("創建test.txt文件")os.WriteFile("test.txt", []byte("測試內容"), 0644)} else {data, _ := os.ReadFile("test.txt")fmt.Println("文件內容:", string(data))}// 6. 退出進程fmt.Println("\n--- 進程退出 ---")defer fmt.Println("清理工作...") // defer語句會在函數退出前執行// os.Exit(0) // 立即退出,不執行defer// syscall.Exit(0) // 系統調用方式退出// 7. 創建子進程fmt.Println("\n--- 創建子進程 ---")attr := &os.ProcAttr{ //創建ProcAttr結構體定義子進程屬性Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}, //Files字段設置子進程的標準輸入/輸出/錯誤流,這里復用父進程的IO}process, err := os.StartProcess("/bin/ls", []string{"ls", "-l"}, attr) //啟動/bin/ls程序執行ls -l命令if err != nil {fmt.Println("啟動失敗:", err)return}fmt.Println("子進程ID:", process.Pid) //輸出子進程 pidstate, _ := process.Wait()fmt.Println("子進程退出狀態:", state.Success()) //檢查退出狀態}
os.Getpid()
- 獲取當前進程IDexec.Command()
- 執行系統命令os.Stat()
- 檢查文件狀態os.StartProcess()
- 創建子進程os.Getenv()
/Setenv()
- 環境變量操作runtime
包 - 獲取運行時信息signal
包 - 處理系統信號(示例中已注釋)os.Exit()
- 控制進程退出
編碼相關
HASH 哈希
- SHA256 Hash :
- https://gobyexample.com/sha256-hashes
- h.write will put value into h and then h.Sum possess together (h" sha256.New())
- HASH need to transform string into []byte
- https://gobyexample.com/sha256-hashes
Base64 encoding 基礎64編碼
- sumup: https://gobyexample.com/base64-encoding
- std and URL two types of encoding (also need byte)
- use for image upload ,SSL,
數據格式轉換和處理
需要轉換和接收成特定的數據類型,方便傳遞 比如int轉換為string
字符串和int之間
主要是 strconv (str-conversion理解)
- 字符串轉到 int以及數字類型之間轉換
package mainimport ("fmt""strconv"
)func main() {// 字符串轉浮點數f, _ := strconv.ParseFloat("1.234", 64)fmt.Println(f) // 1.234// 字符串轉整數(十進制)n, _ := strconv.ParseInt("111", 10, 64)fmt.Println(n) // 111// 字符串轉整數(自動識別進制)n, _ = strconv.ParseInt("0x1000", 0, 64)fmt.Println(n) // 4096// 簡化版字符串轉整數n2, _ := strconv.Atoi("123")fmt.Println(n2) // 123// 錯誤處理示例n2, err := strconv.Atoi("AAA")fmt.Println(n2, err) // 0 strconv.Atoi: parsing "AAA": invalid syntax
}
- int 轉到字符串的方法
package mainimport ("fmt""strconv"
)func main() {// 方法1:strconv.Itoa(僅適用于int)num := 42str1 := strconv.Itoa(num)fmt.Println(str1) // "42"// 方法2:strconv.FormatInt(支持int64和指定進制)str2 := strconv.FormatInt(int64(num), 10) // 十進制fmt.Println(str2) // "42"// 方法3:fmt.Sprintf(靈活但性能略低) 不過也是常用的str3 := fmt.Sprintf("%d", num)fmt.Println(str3) // "42"
}
Itoa 理解成 int to a 字符 這樣記
URLparsing URL解析
- 理解url的格式
- https://adam.herokuapp.com/past/2010/3/30/urls_are_the_uniform_way_to_locate_resources/
- sumup: to get the URL info :https://gobyexample.com/url-parsing