Welcome! In this lesson, we'll delve into the basic string manipulation features of Go, which include string tokenization, string concatenation, trimming of whitespace from strings, and type conversion operations.
In Go, you can split a string into smaller parts, or tokens, using the strings.Split function from the strings package. Here's an example:
In the example above, we use a space as a delimiter to split the sentence into individual words with strings.Split(sentence, " "). Then, the for loop prints each word in the sentence on a new line.
In Go, strings can be concatenated using the + operator:
In this example, we use the + operator to construct a larger string from smaller strings. The += operator appends a string to the existing string, effectively creating a new string with the original content and the appended string. Recall that in Go strings are immutable, so both + and += result in the creation of a new string rather than modifying the original(s).
Additionally, we use the strings.Join function to concatenate elements from a slice of strings (words) into a single string (sentence). Here, a space is used as a separator between each word. This approach is efficient when concatenating multiple strings as it avoids creating multiple intermediate strings.
In Go, you can use the strings.TrimSpace function to remove leading and trailing white spaces from a string:
In this example, strings.TrimSpace is used to remove all leading and trailing whitespaces from a string.
You can convert strings to numbers using functions from the strconv package like strconv.Atoi for integers and strconv.ParseFloat for floats. Conversely, you can convert numbers to strings using strconv.Itoa or fmt.Sprintf:
In this code, we use strconv.Atoi, strconv.ParseFloat, and strconv.Itoa for type conversions. Moreover, we use fmt.Sprintf to format a float (height) as a string with two decimal places by specifying "%.2f".
In Go, your own functions can be created to organize code and reuse logic. A function has a name, parameters (optional), return types, and a body. Here's a basic structure:
In this example, greet is a function that takes a string parameter name and returns a string. The function concatenates the string "Hello, " with the value passed to the name parameter.
Now that we know how to define functions, let's integrate all string operations discussed today into a function:
By integrating these methods within the calculateAverage function:
- We split the string
"1,2,3,4,6"into individual digit characters withstrings.Split(numbers, ","); - We loop over each digit, converting to an integer using
strconv.Atoiand summing them up; - Finally, we calculate the average and return the result.
Great job! You've gained an overview of Go's string manipulation features, including string concatenation, string tokenization, trimming whitespace from strings, type conversions, and an introduction to user-created functions. Now, it's time to get hands-on with these concepts in the exercises that follow. Happy coding!
