Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Print an array or slice elements in Go (Golang)

Posted on May 18, 2023May 18, 2023 by admin

Table of Contents

  • Overview
  • Print a Slice
  • Print a Array

Overview

The way we print an array or slice is same. Let’s look at both one by one

Print a Slice

Print slice elements together

Using

  • fmt.Println and
  • fmt.Printf
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3}
fmt.Println(numbers)
fmt.Printf("Numbers: %v", numbers)
}

Output

[1 2 3]
Numbers: [1 2 3]

Print individual slice elements

Using

  • for-range loop
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3}
for _, num := range numbers {
fmt.Println(num)
}
}

Output

1
2
3

Print a Array

Print array elements together

Using

  • fmt.Println and
  • fmt.Printf
package main
import "fmt"
func main() {
numbers := [3]int{1, 2, 3}
fmt.Println(numbers)
fmt.Printf("Numbers: %v", numbers)
}

Output

[1 2 3]
Numbers: [1 2 3]

Print individual array elements:

Using

  • for-range loop
package main
import "fmt"
func main() {
numbers := [3]int{1, 2, 3}
for _, num := range numbers {
fmt.Println(num)
}
}

Output

1
2
3
  • array
  • slice
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2025 Welcome To Golang By Example | Design: Web XP