Go String

In Go, a string is a sequence of characters. Strings are used to represent text in Go programs, and they are one of the fundamental data types in the language. Strings in Go are represented as a sequence of bytes, and each byte represents a character in the string.

The syntax for declaring a string variable in Go is as follows:

var str string
Source:‮gi.www‬iftidea.com

You can initialize a string variable with a value like this:

str := "hello world"

In this example, a string variable named str is created and initialized with the value "hello world".

You can access individual characters in a string using the square bracket notation:

fmt.Println(str[0])  // prints 'h'

In this example, the first character of the string str is printed to the console.

You can concatenate strings using the + operator:

str1 := "hello"
str2 := "world"
str3 := str1 + " " + str2
fmt.Println(str3)  // prints "hello world"

In this example, two string variables str1 and str2 are concatenated together with a space in between, and the resulting string is assigned to a new variable str3.

You can get the length of a string using the len() function:

fmt.Println(len(str))  // prints the length of the string

In this example, the length of the string str is printed to the console.