Strings
A Go string is a read-only slice of bytes. The language and the standard library treat strings specially - as containers of text encoded in UTF-8, which allows it to represent a wide range of characters from different languages.
package main
import "fmt"
func main() {
// Declaring and initializing a string
// with English letters and Chinese Characters
str := "Hello, 世界"
// Printing the string
fmt.Println("String:", str)
// => String: Hello, 世界
// Accessing individual bytes in a string
char := str[0] // This gives the byte value, not the character
fmt.Printf("Byte Value: %c\n", char)
// => Byte Value: H
// String length
length := len(str)
fmt.Println("String Length:", length)
// => String Length: 13
// 7(Hello, ) + 6(世界) = 13
}
Runes
In other languages, strings are made of “characters”. In Go, the concept of a character is called a rune
.
It is an alias for int32
. Runes are used to represent individual characters in a string, allowing you to work with Unicode characters more easily.
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
// Declaring and initializing a rune
var r rune = '世'
fmt.Printf("Rune: %c\n", r)
// => Rune: 世
str := "Hello, 世界"
// Calculate the length of the string in runes
runeCount := utf8.RuneCountInString(str)
// 7(Hello, ) + 2(世界) = 9
fmt.Printf("String: %s, in bytes: %d, in runes: %d", str, len(str), runeCount)
// => String: Hello, 世界, in bytes: 13, in runes: 9
}
Formatting
The fmt
package provides a variety of functions for formatting strings, similar to the printf
family of functions in C.
package main
import "fmt"
func main() {
name := "John"
age := 30
// Basic string formatting
fmt.Printf("Name: %s, Age: %d\n", name, age)
// Width and Precision
value := 123.456789
fmt.Printf("Default: %f\n", value)
fmt.Printf("Width 8: %8f\n", value)
fmt.Printf("Precision 2: %.2f\n", value)
fmt.Printf("Width 8, Precision 2: %8.2f\n", value)
}
You can also use Sprintf
to format a string and return it for later use.
package main
import "fmt"
func main() {
name := "Alice"
age := 25
// Using Sprintf to format a string
formattedString := fmt.Sprintf("Name: %s, Age: %d", name, age)
// ...
}
Formatting with verbs
The Printf
function family supports various verbs for formatting different types of values.
For example:
%s
: String%b
: Binary%c
: Char%d
: Decimal (integer)%f
: Floating-point%e
: Float in scientific notation%x
: Hex encoding%t
: Boolean%v
: Default format%T
: Type of the value
package main
import "fmt"
type point struct {
x, y int
}
func main() {
p := point{5, 8}
fmt.Printf("struct1: %v\n", p)
fmt.Printf("struct2: %+v\n", p)
fmt.Printf("struct3: %#v\n", p)
fmt.Printf("type: %T\n", p)
fmt.Printf("bool: %t\n", true)
fmt.Printf("int: %d\n", 58)
fmt.Printf("bin: %b\n", 58)
fmt.Printf("char: %c\n", 72)
fmt.Printf("hex: %x\n", 5858)
fmt.Printf("float1: %f\n", 58.9)
fmt.Printf("float2: %e\n", 158800000.0)
fmt.Printf("float3: %E\n", 158800000.0)
fmt.Printf("str1: %s\n", "\"abc\"")
fmt.Printf("str2: %q\n", "\"abc\"")
fmt.Printf("str3: %x\n", "hello")
fmt.Printf("pointer: %p\n", &p)
fmt.Printf("width1: |%6d|%6d|\n", 12, 586)
fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45)
fmt.Printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45)
fmt.Printf("width4: |%6s|%6s|\n", "lite", "r")
fmt.Printf("width5: |%-6s|%-6s|\n", "lite", "rank")
}
Its output:
struct1: {5 8}
struct2: {x:5 y:8}
struct3: main.point{x:5, y:8}
type: main.point
bool: true
int: 58
bin: 111010
char: H
hex: 16e2
float1: 58.900000
float2: 1.588000e+08
float3: 1.588000E+08
str1: "abc"
str2: "\"abc\""
str3: 68656c6c6f
pointer: 0xc0000a4000
width1: | 12| 586|
width2: | 1.20| 3.45|
width3: |1.20 |3.45 |
width4: | lite| r|
width5: |lite |rank |
String Functions
Go provides a variety of functions for working with strings. Many of these functions are part of the standard library's strings
package.
package main
import (
"fmt"
"strings"
)
func main() {
// len - String Length
str3 := "Hello, Literank!"
length := len(str3)
fmt.Println("String Length:", length)
// strings.Contains - Check Substring
contains := strings.Contains(str3, "Golang")
fmt.Println("Contains 'Golang':", contains)
// strings.ToLower and strings.ToUpper - Change Case
lower := strings.ToLower(str3)
upper := strings.ToUpper(str3)
fmt.Println("Lowercase:", lower)
fmt.Println("Uppercase:", upper)
// strings.Split - Split String
str4 := "apple,orange,banana"
parts := strings.Split(str4, ",")
fmt.Println("Split String:", parts)
// strings.Join - Join Strings
joined := strings.Join(parts, ", ")
fmt.Println("Joined String:", joined)
// strings.Replace - Replace Substring
replaced := strings.Replace(str3, "Literank", "World", -1)
fmt.Println("Replaced String:", replaced)
// strings.TrimSpace - Trim Spaces
str5 := " Hello, Golang! "
trimmed := strings.TrimSpace(str5)
fmt.Println("Trimmed String:", trimmed)
}
Code Challenge
Write a Go function that checks whether a given string is a palindrome or not. A palindrome is a string that reads the same backward as forward, ignoring spaces, punctuation, and capitalization.