- Author

- Name
- Nelson Silva
- Social
Introduction
Strings are a fundamental part of any programming language, and in Go they come with a robust set of features that allow a wide range of manipulations.
String Characteristics
Escape Sequences: These sequences allow you to represent special characters in strings.
\n| New line\t| New tab (indentation)
String Manipulation Functions: Go, through the "strings" package, offers a series of functions to manipulate strings.
len(variable)| Returns the number of characters in the stringstrings.ToUpper()| Converts all letters to uppercasestrings.ToLower()| Converts all letters to lowercase
String Interpolation: The
Printf()function in Go allows you to embed values directly into strings using format specifiers.
Other useful operations:
- Concatenation: You can use the
+operator to concatenate two strings.
fullName := "Nelson" + " " + "Silva"
- Substring: Use
[start:end]to obtain a sub-string.
part := "Nelson"[0:3] // Nel
- Comparison: Strings can be compared using the
==and!=operators.
if "Go" == "go" {
// This block will not be executed, as the comparison is case-sensitive.
}
Example
Let's see how some of these features work in practice:
package main
import (
"fmt"
"strings"
)
func main() {
firstName, lastName := "Nelson", "Silva"
age := 28
fmt.Println("Name:", strings.ToUpper(firstName), strings.ToLower(lastName), "\nAge:", age)
fmt.Printf("Name: %s %s\nAge: %d", firstName, lastName, age)
/*
Name: NELSON silva
Age: 28
Name: Nelson Silva
Age: 28
*/
}
Conclusion
Strings are versatile and essential in any application. With the features provided by Go, you can manipulate and manage strings effectively and efficiently. Continuing to experiment and explore Go's capabilities will certainly give you a deeper understanding.