Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
Strings are an essential data type in Go (or Golang). In Go, strings are read-only sequences of bytes, and they are typically used to represent text. This tutorial will guide you through the basics of working with strings in Go.
Strings can be declared in a few ways:
package main import "fmt" func main() { // Using double quotes str1 := "Hello, Golang!" fmt.Println(str1) // Outputs: Hello, Golang! // Using backticks (raw string literals, can span multiple lines) str2 := `Hello, Golang!` fmt.Println(str2) }
Once a string is created, it cannot be modified:
s := "Hello" // s[0] = 'h' // This would throw a compile-time error
To "modify" a string, you typically create a new string with the desired modifications:
s := "Hello" s = "h" + s[1:] fmt.Println(s) // Outputs: hello
The length of a string can be determined using the len
function:
s := "Hello" fmt.Println(len(s)) // Outputs: 5
Individual bytes of a string can be accessed using indices. Remember, this gets you the byte value, not necessarily a character (as characters in UTF-8 can span multiple bytes):
s := "Hello" byteVal := s[1] fmt.Println(byteVal) // Outputs: 101 (byte value for 'e') fmt.Printf("%c\n", s[1]) // Outputs: e
Strings can be concatenated using the +
operator:
hello := "Hello" world := " World" result := hello + world fmt.Println(result) // Outputs: Hello World
Strings in Go are encoded using UTF-8. To iterate over individual characters (runes) in a string, you can use the range
keyword:
s := "Hello, ����" for index, runeValue := range s { fmt.Printf("%d: %c\n", index, runeValue) }
In Go, individual characters are represented using the rune
type, which is an alias for int32
. You can convert a string to a slice of runes:
s := "Hello" runes := []rune(s) runes[0] = 'h' s2 := string(runes) fmt.Println(s2) // Outputs: hello
The strings
package provides many utility functions for working with strings:
import "strings" s := "hello" fmt.Println(strings.ToUpper(s)) // Outputs: HELLO fmt.Println(strings.Contains(s, "ell")) // Outputs: true
+
operator can be used for string concatenation.rune
, which corresponds to a Unicode code point.strings
package offers numerous functions to work with strings.Mastering strings is crucial in any programming language, especially in Go, where they play a pivotal role in many standard library functions and everyday programming tasks.
String manipulation and operations in Golang:
Go provides a rich set of standard library functions for string manipulation, including concatenation, splitting, and indexing.
package main import ( "fmt" "strings" ) func main() { // Concatenation str1 := "Hello" str2 := "World" result := str1 + " " + str2 fmt.Println(result) // Splitting words := strings.Split(result, " ") fmt.Println(words) // Indexing fmt.Println(result[0]) // Accessing the first character }
Working with runes and characters in Golang strings:
Strings in Go are sequences of runes (Unicode code points). Working with individual runes involves converting between strings and runes.
str := "Hello, ����" firstRune := []rune(str)[0] fmt.Printf("First Rune: %c\n", firstRune)
Converting between strings and other types in Golang:
Go provides the strconv
package for converting between strings and other types, such as integers and floats.
import ( "fmt" "strconv" ) func main() { // String to integer str := "42" num, _ := strconv.Atoi(str) fmt.Println(num) // Integer to string num2 := 99 str2 := strconv.Itoa(num2) fmt.Println(str2) }
String formatting and interpolation in Golang:
Go supports string formatting using the fmt
package, allowing you to interpolate values into strings.
package main import "fmt" func main() { name := "Alice" age := 30 fmt.Printf("Name: %s, Age: %d\n", name, age) }
Searching and replacing in Golang strings:
The strings
package provides functions for searching and replacing substrings within strings.
package main import ( "fmt" "strings" ) func main() { text := "Hello, World!" contains := strings.Contains(text, "World") fmt.Println(contains) replaced := strings.Replace(text, "Hello", "Hi", 1) fmt.Println(replaced) }
String comparison and equality in Golang:
Use the ==
operator for string equality comparison. Be cautious with case-sensitive and case-insensitive comparisons.
str1 := "hello" str2 := "Hello" isEqual := str1 == str2 fmt.Println(isEqual)
Handling multi-line strings in Golang:
Multi-line strings can be created using backticks (`) and are often used for raw string literals.
multiLine := ` This is a multi-line string` fmt.Println(multiLine)