将一个十进制数组转换为字符串格式的golang代码。

huangapple go评论115阅读模式
英文:

Convert a decimal array to string format in golang

问题

我可以帮你翻译。你想在Go语言中将一个十进制数组转换为字符串格式的文本,是否有Go语言中支持这个功能的函数?你能给我一个示例代码吗?

例如:

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. func main() {
  8. decimalArray := []int{97, 98, 99}
  9. textArray := make([]string, len(decimalArray))
  10. for i, num := range decimalArray {
  11. textArray[i] = strconv.Itoa(num)
  12. }
  13. text := strings.Join(textArray, " ")
  14. fmt.Println(text)
  15. }

这段代码将一个十进制数组转换为字符串格式的文本。它使用了strconv.Itoa函数将每个十进制数转换为字符串,并使用strings.Join函数将字符串数组连接成一个字符串。最后,它打印出了转换后的文本。

英文:

I want to convert a decimal array to text in string format in golang, is there any function in golang that supports this. Can you give me a sample code?
E.g :

  1. 97 98 99
  2. a b c

答案1

得分: 1

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. a := []int{97, 98, 99}
  7. result := []string{}
  8. for _, element := range a {
  9. result = append(result, string(element))
  10. }
  11. fmt.Println(result)
  12. }

[a b c]

https://go.dev/play/p/WUYIiIcHP16

英文:
  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. a := []int{97, 98, 99}
  7. result := []string{}
  8. for _, element := range a {
  9. result = append(result, string(element))
  10. }
  11. fmt.Println(result)
  12. }

[a b c]

https://go.dev/play/p/WUYIiIcHP16

huangapple
  • 本文由 发表于 2022年3月17日 15:40:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/71508595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定