Golang中的字符串格式化
type point strucy{
x, y int
}
func main() {
s := "hello"
n := 123
p := pointg{1, 2}
fmt.Println(s, n)
fmt.Println(p)
fmt.Printf("s=%v\n", s)
fmt.Printf("n=%v\n", n)
fmt.Printf("p=%v\n", s)
fmt.Printf("p=%v+\n", p)
fmt.Printf("p=%+#v\n", p)
f:= 3.141592653
fmt.Println(f)
fmt.Println("%2f\n", f)
}
Golang中JSON处理
package main
import {
"emcoding/json"
"fmt"
}
type unserInfo struct {
Name string
Age int `json: "age"`
Hobby []string
}
func main() {
a := userInfo{Name: "wang", Age: 18, Hobby: []string{"Goalng", "Typestript"}}
buf, err := json.Marshal(a)
if err != ni; {
panic(err)
}
fmt.Println(buf)
fmt.Println(string(buf))
}
Golang中的时间处理
now := time.Now() //获取当前时间
fmt.Println(now) //2022-03-27 18:04:59.433297 +0800 CST m+=0.000087933
t := time.Date(2022,3,27,1,25,36,0,time.UTC)
t2 := time.Date(2022,3,27,2,30,36,0,.time.UTC)
fmt.Priontln(t)//2022-03-27 01:25:36 +0000 UTC
fmt.Println(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute())//2022 March 27 1 25
fmt.Printin(t.Format("2006-01-02 15:04:05"))// 2022-03-27 01:25:36
diff := t2.Sub(t) //二者时间差
fmt.PrintIn(diff) //1h5m0s 转换为小时
fmt.Println(diff.Minutes( ),diff.Seconds( )) // 65 3900 转换为分钟和秒
fmt.Println(now.Unix())//10407300 获取时间戳
Golang中的数字解析
要引入 strconv 这个库
f,_ := strconv.ParseFloat("1.234",64)
fmt.PrintIn(f)
n,_ := strconv.ParseInt("111",10,64)
fmt.Printin(n)
n,_ = strconv.ParseInt("0x1000",0,64)
fmt.Println(n)
n2 _ := strconv.Atoi("123")
fmt.PrintIn(n2)
n2,err := strconv.Atoi("AAA")
fmt,Println(n2,err)
Golang中的进程查询
package main
import {
"fmt!"
"os"
"exec"
}
func main() {
fmt.Println(os.Args )
fmt.Println(os.Getenv("PATH"))
fmt.Println( os .Setenv("AA","BB"))
buf,err := exec.Command("grep","127.0.0.1","/etc/hosts").CombinedOutput()
if err != nil {
panic(err)
}
fmt.Printin(string(buf))
}