Home>Article>Backend Development> How to convert string to array in go language
The method of converting string to array in Go language: 1. Directly initialize the array, and then convert the string into an array through the "IPAddresses := []string{leftIP}" method; 2. Directly write the string Just put it into an empty array.
Environment of this article: Windows 7 system, Go1.11.2 version, this article is applicable to all brands of computers.
Recommended tutorial: "go language tutorial"
Conversion between arrays and strings in go language
String conversion For the array
1. Creation of the array:
var endpoint = []string{"0.0.0.0:2379"} //直接初始化数组 //数组 <<= string IPAddresses := []string{leftIP}
2. Directly writing into the empty array:
etcd :="sdfsdferf" stringarr := make([]string, 0) //创建空数组 stringarr = append(stringarr, etcd) /*stringarr = append(stringarr, "456")*/ println("*****") for _, str := range stringarr { println(str) } println("*****")
How to convert the array into a string
1. Convert an element in the array directly into a string
arr := make([]string, 0) arr[0] = "sfsdfsdf" string := arr[0] //直接将该数组的一个元素,赋值给字符串 fmt.Printf("====>:%s\n", string)
2. Convert all the data in the array into a string
func arrayToString(arr []string) string { var result string for _, i := range arr { //遍历数组中所有元素追加成string result += i } return result }
More For related technical articles, please visit thegolang tutorialcolumn!
The above is the detailed content of How to convert string to array in go language. For more information, please follow other related articles on the PHP Chinese website!