- flag包
- strings包
- string) bool">func Contains(s, substr string) bool
- string) int
">func Index(s, sep string) int - string) bool">func HasPrefix(s, prefix string) bool
- string) bool">func HasSuffix(s, suffix string) bool
- string, n int) string">func Replace(s, old, new string, n int) string
- string) []string">func Split(s, sep string) []string
- json包
- strconv包
flag包
package main
import (
"flag"
"fmt"
)
func style1() {
var user=flag.String("user","","user字段需要接收一个username")
//Parse()解析命令行参数
flag.Parse()
fmt.Println(*user)
}
func style2() {
var user string
flag.StringVar(&user,"user","","user字段接收一个username")
flag.Parse()
fmt.Println(user)
}
func main() {
//fmt.Println("hello,world")
//fmt.Println(os.Args) //返回[]string [main.exe test]
//style1()
//style2()
}
https://studygolang.com/pkgdoc
strings包
func Contains(s, substr string) bool
判断字符串s是否包含字符或者子串substr
s:="hello,world"
//字符串包含
fmt.Println(strings.Contains(s,"he"))//true
func Index(s, sep string) int
判断子串sep在字符串s中出现的位置,不存在则返回-1。
s:="hello,world"
//字符串出现位置(索引)
fmt.Println(strings.Index(s,"world"))//6
func HasPrefix(s, prefix string) bool
func HasSuffix(s, suffix string) bool
判断字符串s是否以字符串prefix为前缀。
判断字符串s是否以字符串suffix为后缀。
s:="hello,world"
//前缀和后缀
fmt.Println(strings.HasPrefix(s,"he"))//true
fmt.Println(strings.HasPrefix(s,"ld"))//true
func Replace(s, old, new string, n int) string
在字符串s中,把old字符串替换为new字符串,n表示替换的次数,n<0表示全部替换。
s:="hello,world"
//替换
fmt.Println(strings.Replace(s,"l","b",1))//heblo,world
func Split(s, sep string) []string
strings.Split(s, sep) 用于自定义分割符号来对指定字符串进行分割,同样返回 slice
s:="hello,world"
//分割
fmt.Println(strings.Split(s,"l"))//[he o,wor d]
json包
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Server struct {
Servername string `json:"servername"`
Serverip string `json:"serverip"`
Serverport int `json:"serverport"`
}
//json序列化结构
func SerializeStruct() {
sever := new(Server)
sever.Serverip = "127.0.0.1"
sever.Servername = "struct"
sever.Serverport = 8080
bypes, err := json.Marshal(sever) //序列化成json 字节数组
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("marshal json:", string(bypes)) //将json字节数组转换成string
}
//json序列化map
func SerializeMap() {
sever := make(map[string]interface{})
sever["Serverip"] = "192.168.0.1"
sever["Servername"] = "map"
sever["Serverport"] = 9090
bypes, err := json.Marshal(sever) //序列化成json 字节数组
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("marshal json:", string(bypes)) //将json字节数组转换成string
}
//反序列化struct
func UnSerializeStruct() {
jsonString :="{\"servername\":\"struct\",\"serverip\":\"127.0.0.1\",\"serverport\":8080}"
sever := new(Server)
err:=json.Unmarshal([]byte(jsonString),sever)
if err!=nil{
fmt.Println(err.Error())
}
fmt.Println("Unmarshal struct",sever)
}
//反序列化map
func UnSerializeMap() {
jsonString :="{\"servername\":\"map\",\"serverip\":\"192.168.0.1\",\"serverport\":9090}"
sever := make(map[string]interface{})
err:=json.Unmarshal([]byte(jsonString),&sever)
if err!=nil{
fmt.Println(err.Error())
return
}
fmt.Println("Unmarshal map",sever)
}
func main() {
//json包学习
SerializeStruct()
SerializeMap()
UnSerializeStruct()
UnSerializeMap()
}
strconv包
func main() {
//strconv包学习
str:=strconv.Itoa(75) //75(string)
ints,_:=strconv.Atoi(str) //75(int)
fmt.Println(ints)
//解析
fmt.Println(strconv.ParseBool("false"))//false
fmt.Println(strconv.ParseFloat("3.14",64))//3.14
//格式化
fmt.Println(strconv.FormatBool(true))//true(string)
fmt.Println(strconv.FormatInt(10,16))//a base表示转换进制16
}