关闭。这个问题需要details or clarity .它目前不接受答案。
想改进这个问题?通过 editing this post 添加详细信息并澄清问题.
2年前关闭。
Improve this question
package main
import "fmt"
func main() {
var s []int
s = append(s, 2, 3, 4)
printSlice(s)
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
游乐场:https://play.golang.org/p/tx8gmx5eR7B
输出:
len=3 cap=4 [2 3 4]
输出应该是:
2
3
4
我想要它没有循环。
最佳答案
print elements in slice without bracket in Go line by line.
Comment: I want it without loop
例如,
package main
import "fmt"
func printSlice(s []int) {
if len(s) == 0 {
return
}
fmt.Println(s[0])
printSlice(s[1:])
}
func main() {
var s []int
s = append(s, 2, 3, 4)
printSlice(s)
}
游乐场:https://play.golang.org/p/ISXdbjTrfSt
输出:
2
3
4
关于go - 如何在Go中逐行打印没有括号的 slice 中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59177858/