How to print the values of slices

ArraysGo

Arrays Problem Overview


I want to see the values which are in the slice. How can I print them?

projects []Project 	

Arrays Solutions


Solution 1 - Arrays

You can try the %v, %+v or %#v verbs of go fmt:

fmt.Printf("%v", projects)

If your array (or here slice) contains struct (like Project), you will see their details.
For more precision, you can use %#v to print the object using Go-syntax, as for a literal:

%v	the value in a default format.
	when printing structs, the plus flag (%+v) adds field names
%#v a Go-syntax representation of the value

For basic types, fmt.Println(projects) is enough.


Note: for a slice of pointers, that is []*Project (instead of []Project), you are better off defining a String() method in order to display exactly what you want to see (or you will see only pointer address).
See this play.golang example.

Solution 2 - Arrays

For a []string, you can use strings.Join():

s := []string{"foo", "bar", "baz"}
fmt.Println(strings.Join(s, ", "))
// output: foo, bar, baz

Solution 3 - Arrays

I prefer fmt.Printf("%+q", arr) which will print

["some" "values" "list"]

https://play.golang.org/p/XHfkENNQAKb

Solution 4 - Arrays

If you just want to see the values of an array without brackets, you can use a combination of fmt.Sprint() and strings.Trim()

a := []string{"a", "b"}
fmt.Print(strings.Trim(fmt.Sprint(a), "[]"))
fmt.Print(a)

Returns:

a b
[a b]

Be aware though that with this solution any leading brackets will be lost from the first value and any trailing brackets will be lost from the last value

a := []string{"[a]", "[b]"}
fmt.Print(strings.Trim(fmt.Sprint(a), "[]")
fmt.Print(a)

Returns:

a] [b
[[a] [b]]

For more info see the documentation for strings.Trim()

Solution 5 - Arrays

If you want to view the information in a slice in the same format that you'd use to type it in (something like ["one", "two", "three"]), here's a code example showing how to do that:

package main

import (
	"fmt"
	"strings"
)

func main() {
	test := []string{"one", "two", "three"} 	// The slice of data
	semiformat := fmt.Sprintf("%q\n", test)		// Turn the slice into a string that looks like ["one" "two" "three"]
	tokens := strings.Split(semiformat, " ")	// Split this string by spaces
	fmt.Printf(strings.Join(tokens, ", "))		// Join the Slice together (that was split by spaces) with commas
}

Go Playground

Solution 6 - Arrays

fmt.Printf() is fine, but sometimes I like to use pretty print package.

import "github.com/kr/pretty"
pretty.Print(...)

Solution 7 - Arrays

I wrote a package named Pretty Slice. You can use it to visualize slices, and their backing arrays, etc.

package main

import pretty "github.com/inancgumus/prettyslice"

func main() {
	nums := []int{1, 9, 5, 6, 4, 8}
	odds := nums[:3]
	evens := nums[3:]

	nums[1], nums[3] = 9, 6
	pretty.Show("nums", nums)
	pretty.Show("odds : nums[:3]", odds)
	pretty.Show("evens: nums[3:]", evens)
}

This code is going produce and output like this one:

enter image description here


For more details, please read: https://github.com/inancgumus/prettyslice

Solution 8 - Arrays

You could use a for loop to print the []Project as shown in @VonC excellent answer.

package main

import "fmt"

type Project struct{ name string }

func main() {
	projects := []Project{{"p1"}, {"p2"}}
	for i := range projects {
		p := projects[i]
		fmt.Println(p.name) //p1, p2
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfnrView Question on Stackoverflow
Solution 1 - ArraysVonCView Answer on Stackoverflow
Solution 2 - ArraysjgillichView Answer on Stackoverflow
Solution 3 - ArraysPylinuxView Answer on Stackoverflow
Solution 4 - ArraysEddie CurtisView Answer on Stackoverflow
Solution 5 - ArraysGamelubView Answer on Stackoverflow
Solution 6 - ArraysakondView Answer on Stackoverflow
Solution 7 - ArraysInanc GumusView Answer on Stackoverflow
Solution 8 - Arraysuser12817546View Answer on Stackoverflow