1.打印
fmt.Println("asd")
2.字符串与int互转
strconv.Itoa(1) 转字符串
strconv.Atoi("1")转int
3.格式化日期
time.Now().Format("20060102150405")
4.睡眠
time.Sleep(time.Duration(waitTime)*time.Second)
5.随机数
"math/rand"
r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Intn(100) //0-100随机数
6.tcp连接
"net"
...
conn, err := net.Dial("tcp", "ipport")
if err != nil {
fmt.Println("连接服务端失败:", err.Error())
return
}
fmt.Println("已连接服务器")
defer conn.Close()
Client(conn,tcpData)
...
func Client(conn net.Conn,sms string) {
fmt.Println("要发送的消息:"+sms)
conn.Write([]byte(sms))
buf := make([]byte, 2)
c, err := conn.Read(buf)
if err != nil {
fmt.Println("读取服务器数据异常:", err.Error())
}
fmt.Println("服务器返回:"+string(buf[0:c]))
}
7.字符串分割
"strings"
...
strings.FieldsFunc(TOPIC["data"], split)
...
func split(s rune) bool {
if s == ',' {
return true
}
return false
}
8.字符串包含
"strings"
...
fmt.Println(strings.Contains("seafood", "foo")) //true
有疑问加站长微信联系(非本文作者)