The Go String is a sequence of variable-width characters.
Like many other programming languages, strings are also one important type of variable in Go. For example:
var str string = “Welcome to Go World”
Go strings and text files occupy less memory or disk space. Since UTF-8 is standard, Go does not have to encode and decode strings.
Go Strings are value types and immutable. It means if you create a string, you can’t modify the contents of this series.
The first value of a string is empty “”, by default.
The Go platform offers various libraries to manipulate strings. These are:
- unicode
- regexp
- strings
How to create strings in Go Lang?
A simple example of creating a string in the Go language is:
package main import "fmt" func main() { var str = "Welcome to Go World" fmt.Println(str) }
Output:
Welcome to Go World
In the declaration, we know that a string is truly a byte sequence wrapper. In actuality, we can view a string as an (element-immutable) byte slice.
A multi-line string example
The example below shows creating a multi-line string:
package main import ("fmt") func main() { str := `A multi-line string example.` fmt.Println(str) }
Output:
A
multi-line
string
example.
A few string methods
Now, let us look at a few useful string methods. These include len(), ToUpper(), ToLower() and others. See the following section to learn about these.
An example of len()
The example below displays the number of characters (length) of the string:
package main import "fmt" func main() { strEx := "This Tutorial is about Go Strings!" fmt.Println(len(strEx)) }
Output
34
Converting all letters to capitals in a string by ToUpper()
We have three strings with different letters (small and caps). Then we used ToUpper() method as follows:
package main import "fmt" import "strings" func main() { str1 := "small only" str2 := "mIxEd LetterS" str3 := "CAPS ONLY" fmt.Println(strings.ToUpper(str1)) fmt.Println(strings.ToUpper(str2)) fmt.Println(strings.ToUpper(str3)) }
The output:
SMALL ONLY
MIXED LETTERS
CAPS ONLY
Convert all to small by ToLower()
The same strings as above but used ToLower() this time:
package main import "fmt" import "strings" func main() { str1 := "small only" str2 := "mIxEd LetterS" str3 := "CAPS ONLY" fmt.Println(strings.ToLower(str1)) fmt.Println(strings.ToLower(str2)) fmt.Println(strings.ToLower(str3)) }
Output:
small only
mixed letters
caps only
Get the index / position of a character in a string
The example gets the index of a character that starts at 0 in a string:
package main import "fmt" import "strings" func main() { var str = "Hello world!" fmt.Println("Index: ", strings.Index(str, "o")); }
Output:
Index: 4
Using join method example
To concatenate two or more strings, you may use the join method as follows:
package main import "fmt" import "strings" func main() { var str1 = "Hello" str2 := " world!" fmt.Println("Join: ", strings.Join([]string{str1, str2}, " - ")) }
Note: This method is part of the string package that we imported at the beginning of program.
String contains() method example
package main import "fmt" import "strings" func main() { var str = "You are going to like it" fmt.Println(strings.Contains(str,"like")) }
Output:
true