{

Golang Example - Print values of Array or slice


Golang Example - Print values of Array or slice In these examples, You will learn different ways to print/display array or slice elements to console in Go Language.

Following programs are covered.

  • Print Array using For Loop with range
  • Display Slice using PrintF function format
  • Array slice console print without Square brackets([])
  • Print Address or pointer of an array
  • Print Slice of bytes in Binary Form

To understand this example, You should have the following features in the Go language.

Example program: Print an Array or slice using For loop with range

In the below program,

Declared slice and array and initialize values using shorthand assignment operator.

For a loop with a range, the form uses to iterate the elements of an array or slice.

Each iteration has a value and prints the values to console using Printf function with formatting options( %d for int, %s for strings).

Following is a code for print a slice using for loop in golang.

package main  
import "fmt"  
  
func main() {  
  
 slice := []int{1, 2, 3}  
 array := [5]string{"one", "two", "three", "four", "five"}  
  
 for _, value := range slice {  
  fmt.Printf("%d\n", value)  
 }  
 for _, str := range array {  
  fmt.Printf("%s\n", str)  
 }  
}  

The above program output is

1  
2  
3  
one  
two  
three  
four  
five  

Example program2: Display Array or slice elements using Printf function

In this program, Array or slice is printed to console using fmt Printf function with format strings.

Printf Format Strings for Displaying Array or Slice
For example, Array or slice contains {“one”, “two”, “three”}.

We will see different ways of printing values.

  • %v - value in default format ie [one two three]
  • %+v - Value in default format + Displays sign for numerics - [one two three]
  • %#v - Data values in Go Styles - []string{“one”, “two”, “three”}
  • %+q - For Numerics, Displays ASCII code - ['\x01' ‘\x02’ ‘\x03’ ‘\x05’ ‘\f’], Others default format - [“one” “two” “three”]

You can check more printf options

Here is an example display array or slice values to console

package main  
import "fmt"  
  
func main() {  
 sliceStrings := []string{"one", "two", "three"}  
 arrayInt := [5]int{1, 2, 3, 5, 12}  
  
 // Print slice to console  
 fmt.Printf("%v\n", sliceStrings)  
 fmt.Printf("%+v\n", sliceStrings)  
 fmt.Printf("%#v\n", sliceStrings)  
 fmt.Printf("%+q\n", sliceStrings)  
 // Print array to console  
 fmt.Printf("%v\n", arrayInt)  
 fmt.Printf("%+v\n", arrayInt)  
 fmt.Printf("%#v\n", arrayInt)  
 fmt.Printf("%+q\n", arrayInt)  
  
}  

Output:

[one two three]  
[one two three]  
[]string{"one", "two", "three"}  
["one" "two" "three"]  
[1 2 3 5 12]  
[1 2 3 5 12]  
[5]int{1, 2, 3, 5, 12}  
['\x01' '\x02' '\x03' '\x05' '\f']  

Example Program 3 to Print Array or slices without Squrebrackets

As you see in the above program, the Printing array or slice to console contains values wrapped inside Square brackets. Another way, We have to use fmt.Sprint function to remove square brackets from the output. and the result is trimmed using the strings Trim() function.

Here is an example program to print array or slice to console without square brackets([])

package main  
import (  
 "fmt"  
 "strings"  
)  
  
func main() {  
 sliceStrings := []string{"one", "two", "three"}  
 arrayInt := [5]int{1, 2, 3, 5, 12}  
  
 // Print slice without squrebrackets to console  
 fmt.Println(strings.Trim(fmt.Sprint(sliceStrings), "[]"))  
  
 // Print array without squrebrackets to console  
 fmt.Println(strings.Trim(fmt.Sprint(arrayInt), "[]"))  
  
}  

Output:

one two three  
1 2 3 5 12  

Example Program 4 Print address or pointer of array/slice

In Golang, Creating an Array or slice variable involves location or address in memory.

To check the address of an array or slice, we have to use &(ampersand) and array/slice variable name
Here is an example program for printing the pointer of an array

package main  
  
import (  
 "fmt"  
)  
  
func main() {  
 sliceStrings := []string{"one", "two", "three"}  
 arrayInt := [5]int{1, 2, 3, 5, 12}  
 fmt.Printf("Pointer or address of slice %p add array %p \n", &sliceStrings, &arrayInt)  
  
}  

Output:

Pointer or address of slice 0xc00004a400 add array 0xc000076030  

Example Program 5 Print Array or slice bytes in binary bits form

Here is a sequence of steps to print binary data from Array or Slice of data.

  • Declared Byte slice
  • using for loop Print the binary form using Printf with %08b format
package main  
import (  
 "fmt"  
)  
  
func main() {  
  
 byteArray := []byte{25, 1}  
 for _, value := range byteArray {  
  fmt.Printf("%08b", value)  
 }  
  
}  

Output:

0001100100000001  

Conclusion

In this tutorial, You learned multiple examples and ways to iterate and display array and slice using fmt printf for loop and print the address and bytes.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.