Home  >  Article  >  Backend Development  >  How to convert array to string in go language

How to convert array to string in go language

王林
王林Original
2021-02-04 13:59:3118204browse

The method of converting an array into a string in Go language: first traverse all the elements in the array; then append them to string, such as [for _, i := range arr { result = i}].

How to convert array to string in go language

The operating environment of this article: windows10 system, Go 1.11.2, thinkpad t480 computer.

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
   }

Related recommendations: golang tutorial

The above is the detailed content of How to convert array to string in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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 admin@php.cn
Previous article:Is go a weak language?Next article:Is go a weak language?