How to convert string to array in go language

藏色散人
Release: 2023-01-11 09:23:11
Original
13712 people have browsed it

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.

How to convert string to array in go language

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}
Copy after login

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("*****")
Copy after login

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)
Copy after login

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
   }
Copy after login

More For related technical articles, please visit the golang tutorial column!

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!