GO 语言进阶之 进程 OS与 编码,数据格式转换
更多个人笔记见:
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