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

How to convert array to string in go language

王林
Release: 2023-01-13 00:21:41
Original
18224 people have browsed it

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)
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

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!

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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template