
Strings in GoLang
Strings in GoLang
In Go, a string is a sequence of Unicode characters (text). Strings in Go are immutable, meaning once a string is created, it cannot be changed. However, you can create new strings by performing operations like concatenation or slicing.
Key Features of Strings in Go:
- Immutable: Strings cannot be modified once they are created. Modifications on a string will return a new string.
- UTF-8 Encoded: Strings in Go are encoded in UTF-8 format, which allows them to represent a wide range of characters from different languages.
- Sliceable: You can slice a string just like you do with arrays or slices.
- Built-in Functions: Go provides many built-in functions for string manipulation (e.g., concatenation, length, splitting, etc.).
Declaring Strings
You can declare strings in Go using either double quotes ("
) for a regular string or backticks (`
) for raw strings (multiline or with special characters).
import "fmt"func main() { str := package main
import "fmt"func main() { str := package main
import "fmt"func main() { str := package main
import "fmt"func main() { str1 := package main
import ( "fmt" "strings")func main() { words := []package main
import "fmt"func main() { str := package main
import "fmt"func main() { str1 := package main
import ( "fmt" "strings")func main() { str := package main
import ( "fmt" "strings")func main() { str := package main
import ( "fmt" "strings")func main() { str := package main
import ( "fmt" "strings")func main() { str := package main
import ( "fmt" "strings")func main() { str := package main
import ( "fmt" "strings")func main() { str := package main
import "fmt"func main() { rawString := package main
import "fmt"func main() { age := 25 name := "Alex" formattedStr := fmt.Sprintf("Name: %s, Age: %d", name, age) fmt.Println(formattedStr) // Output: Name: Alex, Age: 25}
Summary
- Strings are immutable sequences of characters in Go.
- Strings are UTF-8 encoded and can store any Unicode character.
- Operations on strings include slicing, concatenation, and transformation (like upper/lower case).
- The
strings
package provides many functions for common string manipulation tasks. - String literals can be declared using double quotes (
"
) or backticks (`
) for raw strings.
Strings are one of the most commonly used types in Go, and mastering their usage is essential for any Go developer.