go 分割字符串的三种方式

481 阅读1分钟

Split by whitespace and newline 好用的一种,如果分割是空白

Use the strings.Fields function to split a string into substrings removing any space characters, including newlines.

s := strings.Fields(" a \t b \n")
fmt.Println(s)
// Output: [a b]

Split on comma or other substring

Use the strings.Split function to split a string into its comma separated values.

s := strings.Split("a,b,c", ",")
fmt.Println(s)
// Output: [a b c]

原文 yourbasic.org/golang/spli…