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
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
", i)
}
return str
}
func main() {
slice := []int{1, 2, 3}
fmt.Print(VSlice(slice))
}
Output:
1
2
3
You can also use strings.Join
stringSlices := strings.Join(slice[:], "
")
fmt.Print(stringSlices)