数组的字符串表示

在以下代码中,该部分fmt.Println(string(a)+"\n")不起作用。有没有一种通用的方法来获取像数组这样的构造的字符串表示形式?


package main


import "fmt"


func main() {

    //Array of size 10

    fmt.Println("Original array")

    var a [10]int

    fmt.Println(a)

    a[2] = 2


    //worked

    fmt.Printf("%v\n\n", a)


    //not working - anyway to get string representation of a?

    fmt.Println(string(a)+"\n")


}

为了更准确地说,给定一个对象obj,我们可以在 python 中使用字符串表示str(obj),在 Java 中使用obj.toString(),我想找出 Go 中的对应物是什么。


开心每一天1111
浏览 176回答 2
2回答

天涯尽头无女友

我希望这是你想要的。我也对代码进行了注释,以便您理解。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strconv")// type alias for [10]inttype istr [10]int// istr implements fmt.Stringer interface// String formats the integer array to a string arrayfunc (a istr) String() string {&nbsp; &nbsp; var s string = "["&nbsp; &nbsp; for i := 0; i < 9; i++ {&nbsp; &nbsp; &nbsp; &nbsp; s += strconv.Itoa(a[i]) + " "&nbsp; &nbsp; }&nbsp; &nbsp; s += strconv.Itoa(a[9]) + "]"&nbsp; &nbsp; return s}func main() {&nbsp; &nbsp; // Original&nbsp; &nbsp; var a [10]int&nbsp; &nbsp; fmt.Println("Original Array")&nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp; // Modified&nbsp; &nbsp; a[2] = 2&nbsp; &nbsp; fmt.Println("Modified Array")&nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp; // Expected&nbsp; &nbsp; var b istr&nbsp; &nbsp; b[2] = 2&nbsp; &nbsp; fmt.Println("String Array")&nbsp; &nbsp; fmt.Println(b)}更新:添加可以扩展更多类型的通用解决方案。目前,它可以将 rune/int/bool 的 slice 转换为 string。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "strings")func ToString(iv interface{}) string {&nbsp; &nbsp; var sb strings.Builder&nbsp; &nbsp; switch iv := iv.(type) {&nbsp; &nbsp; case []int:&nbsp; &nbsp; &nbsp; &nbsp; var length = len(iv)&nbsp; &nbsp; &nbsp; &nbsp; if length == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "[]"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune('[')&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < length-1; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(strconv.Itoa(iv[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune(' ')&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(strconv.Itoa(iv[length-1]))&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune(']')&nbsp; &nbsp; case []bool:&nbsp; &nbsp; &nbsp; &nbsp; var length = len(iv)&nbsp; &nbsp; &nbsp; &nbsp; if length == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "[]"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune('[')&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < length-1; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(strconv.FormatBool(iv[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune(' ')&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteString(strconv.FormatBool(iv[length-1]))&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune(']')&nbsp; &nbsp; case []rune:&nbsp; &nbsp; &nbsp; &nbsp; var length = len(iv)&nbsp; &nbsp; &nbsp; &nbsp; if length == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "[]"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune('[')&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < length-1; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune(iv[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune(' ')&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune(iv[length-1])&nbsp; &nbsp; &nbsp; &nbsp; sb.WriteRune(']')&nbsp; &nbsp; }&nbsp; &nbsp; return sb.String()}func main() {&nbsp; &nbsp; // Original&nbsp; &nbsp; var a []int = make([]int, 10)&nbsp; &nbsp; fmt.Println("Original Slice")&nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp; // Modified&nbsp; &nbsp; a[2] = 2&nbsp; &nbsp; fmt.Println("Modified Slice")&nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp; // String&nbsp; &nbsp; fmt.Println("String Slice")&nbsp; &nbsp; fmt.Println(ToString(a))&nbsp; &nbsp; // Boolean&nbsp; &nbsp; fmt.Println("Boolean Slice")&nbsp; &nbsp; var b []bool = make([]bool, 10)&nbsp; &nbsp; b[2] = true&nbsp; &nbsp; fmt.Println(ToString(b))&nbsp; &nbsp; fmt.Println("Rune Slice")&nbsp; &nbsp; var r []rune = make([]rune, 10)&nbsp; &nbsp; r[0] = 'a'&nbsp; &nbsp; r[1] = 'c'&nbsp; &nbsp; fmt.Println(ToString(r))}

翻翻过去那场雪

包的任何变量都会检查其Stringer接口的实现。如果它实现了Stringer接口,它会自动隐式调用String函数。package mainimport (&nbsp; &nbsp; "fmt")// type alias for [10]inttype StringerIntArray [10]int// String formats the StringerIntArray ([10]int) to a stringfunc (a StringerIntArray) String() string {&nbsp; &nbsp; var s string = "My Array is: "&nbsp; &nbsp; for i := 0; i < 9; i++ {&nbsp; &nbsp; &nbsp; &nbsp; s += fmt.Sprint(a[i]," ")&nbsp; &nbsp; }&nbsp; &nbsp; s = s[:len(s)-1]&nbsp; &nbsp; s+=" With Length:"+fmt.Sprint(len(a))&nbsp; &nbsp; return s}func main() {&nbsp; &nbsp; // Original&nbsp; &nbsp; var a [10]int&nbsp; &nbsp; fmt.Println("Original Array")&nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp; // Modified&nbsp; &nbsp; a[2] = 2&nbsp; &nbsp; fmt.Println("Modified Array")&nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp; // Expected&nbsp; &nbsp; var b StringerIntArray&nbsp;&nbsp; &nbsp; b[2] = 2&nbsp; &nbsp; fmt.Println("String Array")&nbsp; &nbsp; fmt.Println(b)}https://play.golang.org/p/lE5gnHXGGF4内置类型定义了默认的字符串格式;如果你需要修改它,你必须创建一个自定义类型并附加一个字符串函数同样通过阅读您的评论,我想添加 fmt 包知道如何格式化不同类型的数据,但有时您想制作输出字符串;是的,fmt 的输出是字符串,如果使用 Sprint、Sprintf 函数,您可以取回字符串你可以在这里阅读更多关于它的信息 https://tour.golang.org/methods/17 https://golang.org/pkg/fmt/#Stringer
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go