This is a creation in Article, where the information may have evolved or changed.
1. Printing
fmt.Println("asd")
2. The string and int are transferred to each other
strconv.Itoa(1) 转字符串strconv.Atoi("1")转int
3. Formatting dates
time.Now().Format("20060102150405")
4. Sleep
time.Sleep(time.Duration(waitTime)*time.Second)
5. Random number
"math/rand"r := rand.New(rand.NewSource(time.Now().UnixNano()))r.Intn(100) //0-100随机数
6.TCP Connection
"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. String segmentation
"strings"...strings.FieldsFunc(TOPIC["data"], split)...func split(s rune) bool { if s == ',' { return true } return false}
8. String contains
"strings"... fmt.Println(strings.Contains("seafood", "foo")) //true