vg

//1.18以前
func TestMyCut(tt *testing.T) {
    url := "127.0.0.1:8080"
    pos := strings.Index(url, ":")
    if pos == -1 {
        fmt.Println("not found")
    } else {
        ip := url[:pos]
        port := url[pos+1:]
        fmt.Println("ip: ", ip)
        fmt.Println("port: ", port)
    }
}

//1.18以后
func TestCut(tt *testing.T) {
    //func Cut(s, sep string) (before, after string, found bool)
    url := "127.0.0.1:8080"
    ip, port, found := strings.Cut(url, ":")
    if found {
        fmt.Println("ip: ", ip)
        fmt.Println("port: ", port)
    } else {
        fmt.Println("not found")
    }

}

发表回复

您的电子邮箱地址不会被公开。

Captcha Code