String Types are used to store text like data.A string is an immutable sequence of bytes.
Go uses UTF-8 encoding to store strings.
A character in UTF8 can be from 1 to 4 bytes long. UTF-8 can represent any character in the Unicode standard. UTF-8 is backwards compatible with ASCII. UTF-8 is the preferred encoding for e-mail and web pages - W3Schools.com
Strings are declared using one of the following ways:
main.govar str1 string = "Hello, World!"var str2 = "Hello, World!"str3 := "Hello, World!"
Zero value for string is empty string
len function can be used to get number of bytes in string
main.gofmt.Printf("length of str1 is %d \n", len(str1))
We can easily create substring from existing string using
s[i,j].This operation always generates new string since string are immutable.
i indicates starting index for substring including
j indicates end index excluding
main.gostr4 := str1[7:13]fmt.Println(str4)