Tuesday, December 18, 2018

Golang Example - Print values of Array or slice

In thsi 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 Squarebrackets
  • Print Address  or pointer of array
  • Print Slice of bytes in Binary Form
To understand this example, You should have following features in Go language

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

In the below program, Declared slice and array with initlized values using shorthand assignment operator.

For loop with range form is used to iterate the elements of an array or slice.
Each iteration has a value and print the values to console using Printf function formatting options( %d for int, %s for strings)
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  elements of an Array or slice 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 is
[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 seen the above program, Printing array or slice to console contains Squrebrackets.To remove Squarebrackets, We have to use fmt.Sprint function remove squarebrackets and result is trimmed using strings Trim() function
Here is an example program to remove squarebrakcets of array or slice printing
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 is
one two three
1 2 3 5 12

Example Program 4 Print address or pointer of array/slice

Creatingn Array or slice variable involves location or address in memory.
To check address of array or slice, we have to use &(ampersand) and array/slice variable name
Here is a example program for printing pointer
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 is
Pointer or address of slice 0xc00004a400 add array 0xc000076030

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

Here is 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 is
0001100100000001

Related article


EmoticonEmoticon