An overview of the

It is also possible to create a fragment or array of string data types in Golang. In fact, fragments or arrays of any data type can be created in Go. This tutorial contains simple examples of creating a fragment or array of string data types in Golang.

It should be added here that in Golang, arrays are fixed size, while fragments can have variable size. More details here

Array – https://golangbyexample.com/understanding-array-golang-complete-guide/

Slice – https://golangbyexample.com/slice-in-golang/

A fragment of a string

package main

import "fmt"

func main(a) {

	//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)
	}
}
Copy the code

The output

Output for First slice of string
abc
def
ghi

Output for Second slice of string
ghi
def
abc
Copy the code

There are two ways to create fragments 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")
Copy the code

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

string_second := make([]string.3)
string_second[0] = "ghi"
string_second[1] = "def"
string_second[2] = "abc"
Copy the code

Either way. That’s how we create the string slice

String array

package main

import "fmt"

func main(a) {

	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)
	}
}
Copy the code

The output

Output for First Array of string
abc
def
ghi

Output for Second Array of string
ghi
def
abc
Copy the code

There are two ways to create arrays. The first way is

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

In the second way, we simply initialize the array with some strings

string_second := [3]string{
	"ghi"."def"."abc",}Copy the code

Take a look at our advanced Golang tutorial. This series of tutorials is carefully designed and we try to cover all the concepts with examples. This tutorial is for those who wish to gain professional knowledge and a solid understanding of Golang – Golang Advanced Tutorial

If you are interested in learning how to implement all design patterns in Golang. If so, then this article is for you — All Design Patterns Golang

The postCreate Slice or Array of Strings in Go (Golang)appeared first onWelcome To Golang By Example.