Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Create Slice or Array of Strings in Go (Golang)

Posted on July 17, 2023July 17, 2023 by admin

Table of Contents

  • Overview
  • Slice of String
  • Array of Strings

Overview

It is possible to create a slice or array of string data type in Golang as well. In fact, a slice or array can be created of any data type in Go. This tutorial contains simple examples to create a slice or array of string data type in golang.

Just to add here that in golang array is of fixed size and slice can have variable size. More details here

Array – /understanding-array-golang-complete-guide/

Slice – /slice-in-golang/

Slice of String

package main
import "fmt"
func main() {
//First Way
var string_first []string
string_first = append(string_first, "abc")
string_first = append(string_first, "def")
string_first = append(string_first, "ghi")
fmt.Println("Output for First slice of string")
for _, c := range string_first {
fmt.Println(c)
}
//Second Way
string_second := make([]string, 3)
string_second[0] = "ghi"
string_second[1] = "def"
string_second[2] = "abc"
fmt.Println("\nOutput for Second slice of string")
for _, c := range string_second {
fmt.Println(c)
}
}

Output

Output for First slice of string
abc
def
ghi
Output for Second slice of string
ghi
def
abc

We have two ways of creating a slice of strings. The first way is

var string_first []string
string_first = append(string_first, "abc")
string_first = append(string_first, "def")
string_first = append(string_first, "ghi")

In the second way, we use make command to create a slice of strings

string_second := make([]string, 3)
string_second[0] = "ghi"
string_second[1] = "def"
string_second[2] = "abc"

Either way works. This is how we can create a slice of strings

Array of Strings

package main
import "fmt"
func main() {
var string_first [3]string
string_first[0] = "abc"
string_first[1] = "def"
string_first[2] = "ghi"
fmt.Println("Output for First Array of string")
for _, c := range string_first {
fmt.Println(c)
}
string_second := [3]string{
"ghi",
"def",
"abc",
}
fmt.Println("\nOutput for Second Array of string")
for _, c := range string_second {
fmt.Println(c)
}
}

Output

Output for First Array of string
abc
def
ghi
Output for Second Array of string
ghi
def
abc

We have two ways of creating an array. The first way is

var string_first [3]string
string_first[0] = "abc"
string_first[1] = "def"
string_first[2] = "ghi"

In second way we directly initialize the array with some strings

string_second := [3]string{
"ghi",
"def",
"abc",
}

Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –All Design Patterns Golang

  • go
  • golang
  • 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

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