In this Tutorial -:

Introduction
Strings are the fundamental building blocks of text-based data in programming. They play a crucial role in various operations, from displaying information to manipulating data.
In Go programming, strings are treated as a primary data type, and understanding their basic operations is essential for any developer.
In this tutorial, we will explore the core concepts of strings in Go and learn how to perform basic operations on them.
Prerequisites
Before we delve into the goto statement in Go, make sure you have Go installed on your system. If you haven’t already done so, you can download the latest version of Go from the official website: https://golang.org/dl/ Once installed, verify the installation by opening a terminal or command prompt and running the following command:
go version
If Go is installed correctly, it will display the version number.
Creating and Initializing Strings
In Go, a string is a sequence of characters enclosed in double quotes. You can create and initialize a string like this:
package main
import "fmt"
func main() {
// Creating and initializing strings
greeting := "Hello, Go!"
fmt.Println(greeting)
}
O/P
Hello, Go!
Concatenating Strings
You can concatenate strings using the + operator:
package main
import "fmt"
func main() {
firstName := "John"
lastName := "Doe"
fullName := firstName + " " + lastName
fmt.Println("Full Name:", fullName)
}
O/P
Full Name: John Doe
String Length
To get the length of a string, you can use the built-in len() function:
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
text := "Go Programming"
length := len(text)
fmt.Println("Length:", length)
// Length of string with non-ASCII characters
nonASCII := "こんにちは"
runeCount := utf8.RuneCountInString(nonASCII)
fmt.Println("Rune Count:", runeCount)
}
O/P
Length: 14
Rune Count: 5
Slicing Strings
You can extract a portion of a string using slicing:
package main
import "fmt"
func main() {
text := "Hello, World!"
slice := text[0:5] // Slicing from index 0 to 4
fmt.Println("Slice:", slice)
}
O/P
Slice: Hello
Slicing for Reversal
You can reverse a string using slicing:
package main
import "fmt"
func main() {
text := "Hello, Go!"
reversed := ""
for i := len(text) - 1; i >= 0; i-- {
reversed += string(text[i])
}
fmt.Println("Reversed:", reversed)
}
O/P
Reversed: !oG ,olleH
Strings and their basic operations are fundamental in Go programming. In this tutorial, we covered creating, concatenating, finding length, and slicing strings. Armed with this knowledge, you can manipulate strings effectively and build powerful applications in Go. Happy coding!
Remember, this is just the beginning. As you explore Go further, you’ll uncover more advanced string operations and techniques that can help you become a proficient Go developer.
