go - 如何在Go中逐行打印没有括号的 slice 中的元素?

标签 go

关闭。这个问题需要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/

相关文章:

pointers - 扫描不工作

string - 如何将多个字符串和int合并成一个字符串

amazon-web-services - 使用无服务器调用函数时传递 JSON

go - go中是否有等效的atof?

go - 调用 struct literal 的方法

go - ...interface{} 函数参数中的 "Dereference"个元素

debugging - 无法调试二进制文件 - "could not launch process: could not find .debug_line section in binary"

json - 在 golang 中为 JSON 结构创建接口(interface)

go - go map值和nil之间的差异

go - `(*RawMessage)(nil)` 是什么意思?