如何在控制台上将切片内容打印在新行上?

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

how to print slice content on new line on console?

问题

有没有办法将切片的每个元素存储在新的一行上?就像这样:

> 1
>
> 2
>
> 3

而不是

> 1
> 2
> 3

我只是不想将这些元素打印在新的一行上,而是想将每个元素存储在单独的一行上。

以下是代码:

package main

import "fmt"

func main() {
    slice := []int{1, 2, 3}
    for _, element := range slice {
        fmt.Println(element)
    }
}

谢谢

英文:

is there any way to store each element of slice on new line ?
something like this :

> 1
>
> 2
>
> 3

instead of

> 1
> 2
> 3

i just dont want to print these elements on new line but want to store elements each on separate line

here is the code:

package main

import "fmt"

func main() {
 

slice := []int{1, 2, 3}
fmt.Println(slice)

}

Thanks

答案1

得分: 5

例如,

package main

import "fmt"

type VSlice []int

func (s VSlice) String() string {
    var str string
    for _, i := range s {
        str += fmt.Sprintf("%d\n", i)
    }
    return str
}

func main() {
    slice := []int{1, 2, 3}
    fmt.Print(VSlice(slice))
}

输出:

1
2
3
英文:

For example,

package main

import "fmt"

type VSlice []int

func (s VSlice) String() string {
	var str string
	for _, i := range s {
		str += fmt.Sprintf("%d\n", i)
	}
	return str
}

func main() {
	slice := []int{1, 2, 3}
	fmt.Print(VSlice(slice))
}

Output:

1
2
3

答案2

得分: 5

你也可以使用strings.Join函数。

stringSlices := strings.Join(slice[:], "\n\n")
fmt.Print(stringSlices)

这段代码的作用是将切片中的字符串元素使用指定的分隔符进行连接,并将结果打印出来。

英文:

You can also use strings.Join

stringSlices := strings.Join(slice[:], "\n\n")
fmt.Print(stringSlices)

答案3

得分: 0

我对两种方法中哪种更快——使用for循环打印切片,还是连接切片然后打印结果——很感兴趣,所以我进行了测试。结果显示,打印切片通常会稍微快一些,尽管有时候使用strings.Join会稍微快一些。

以下是代码:

func printSlice(s[] string) {
    for _, el := range s {
        fmt.Println(el)
    }
}

func joinSlice(s[] string) {
    joinedSlice := strings.Join(s[:], "\n")
    fmt.Println(joinedSlice)
}

func main() {
    start := time.Now()
    printSlice(dirs)
    printTime := time.Since(start)

    start = time.Now()
    joinSlice(dirs)
    joinTime := time.Since(start)

    fmt.Printf("printSlice: %v \njoinSlice: %v \nprintSlice faster?: %v\n", printTime, joinTime, printTime < joinTime)
}

"dirs"是一个包含文件列表的切片,本例中有超过140个文件。以下是10次测试的执行时间(我不知道在Go语言中有更优雅的测试方法,如果有人知道,请随意提供):

1
printSlice: 1.584753ms
joinSlice: 39.132467ms
printSlice faster?: true

2
printSlice: 1.195512ms
joinSlice: 30.53137ms
printSlice faster?: true

3
printSlice: 1.355297ms
joinSlice: 80.888305ms
printSlice faster?: true

4
printSlice: 1.347343ms
joinSlice: 32.668568ms
printSlice faster?: true

5
printSlice: 1.317251ms
joinSlice: 31.575808ms
printSlice faster?: true

6
printSlice: 1.038342ms
joinSlice: 27.515216ms
printSlice faster?: true

7
printSlice: 34.409332ms
joinSlice: 8.32355ms
printSlice faster?: false

8
printSlice: 1.20479ms
joinSlice: 30.070951ms
printSlice faster?: true

9
printSlice: 1.479724ms
joinSlice: 34.551614ms
printSlice faster?: true

10
printSlice: 977.179μs
joinSlice: 442.46μs
printSlice faster?: false

所以在10次测试中,有8次printSlice更快。其中一次测试的差异是微秒级别的,另一次是毫秒级别的。如果有人知道更好的重复此测试的方法,请随意编辑或提出建议。

英文:

I was interested in which of the two methods is faster - printing a slice using a for loop, or joining a slice and then printing the result, so I tested it. Turns out printing is usually slightly faster, although sometimes there's a marginal difference in favour of using strings.Join.

Here's the code:

func printSlice(s[] string) {
	for _, el := range s {
		fmt.Println(el)
	}
}

func joinSlice(s[] string) {
	joinedSlice := strings.Join(s[:], &quot;\n&quot;)
	fmt.Println(joinedSlice)
}

func main() {
    start := time.Now()
    printSlice(dirs)
    printTime := time.Since(start)

    start = time.Now()
    joinSlice(dirs)
    joinTime := time.Since(start)

    fmt.Printf(&quot;printSlice: %v \njoinSlice: %v \nprintSlice faster?: %v\n&quot;, printTime, joinTime, printTime &lt; joinTime)
}

"dirs" is a slice containing a list of files - in this case over 140 files. Here's the execution time over 10 tests (I'm not aware of a more elegant way to test this in GoLang, but if anyone knows, feel free.)

1
printSlice: 1.584753ms 
joinSlice: 39.132467ms 
printSlice faster?: true

2
printSlice: 1.195512ms 
joinSlice: 30.53137ms 
printSlice faster?: true

3
printSlice: 1.355297ms 
joinSlice: 80.888305ms 
printSlice faster?: true

4
printSlice: 1.347343ms 
joinSlice: 32.668568ms 
printSlice faster?: true

5
printSlice: 1.317251ms 
joinSlice: 31.575808ms 
printSlice faster?: true

6
printSlice: 1.038342ms 
joinSlice: 27.515216ms 
printSlice faster?: true

7
printSlice: 34.409332ms 
joinSlice: 8.32355ms 
printSlice faster?: false

8
printSlice: 1.20479ms 
joinSlice: 30.070951ms 
printSlice faster?: true

9
printSlice: 1.479724ms 
joinSlice: 34.551614ms 
printSlice faster?: true

10
printSlice: 977.179&#181;s 
joinSlice: 442.46&#181;s 
printSlice faster?: false

So in 8 out of 10 tests, printSlice was faster. The difference came down to microseconds in one test, milliseconds in another. If anyone knows a better way to repeat this test, feel free to edit or make a suggestion.

huangapple
  • 本文由 发表于 2017年8月23日 19:10:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/45838110.html
匿名

发表评论

匿名网友

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

确定