SoFunction
Updated on 2025-03-04

Golang string type principle and its usage method

Go strings are immutable byte sequences, asUTF-8EncodedUnicodeCoding point. The internal sequence of strings cannot be changed. This design is mainly to reduce memory expenses. Both strings and their substrings can use the same underlying memory.

ASCII code, Unicode characters, UTF-8 encoding

ASCII code:Use 7 digits to represent 128 strings (lower and lower case English letters, numbers, various punctuation points and device control characters).
Unicode characters:Basically, it includes all text characters in the world, and uses the int32-bit data type to save a single character. GoruneThe data type corresponds to the type.
UTF-8 encoding:Unicode code points are variable-length encoding in bytes, which is compatible with ASCII codes, which is equivalent to assembling character types of different byte sizes together to make the encoding more compact. However, UTF-8 encoded strings cannot directly access a certain character according to the following table, and need to be processed in a special way.

Basic operations of string data types

1. String length

len function:len(str) This function can obtain the number of bytes occupied by a string. If the string is a letter or a number, it can represent the number of characters in the string. If it containsSpecial characters such as Chinese charactersIt cannot represent the number of characters. Need to use it now(str) function

package main
import (
	"fmt"
	"unicode/utf8"
)
func main() {
	str1 := "abcd"
	str2 := "abc"
	("len(str1) -> %d\n", len(str1))
	("len(str2) -> %d\n", len(str2))
	("Number of characters(str1) -> %d\n", (str1))
	("Number of characters(str2) -> %d\n", (str2))
}
// Output// len(str1) -> 4
// len(str2) -> 6
// Number of characters (str1) -> 4// Number of characters(str2) -> 4

2. Problems of string prefix and substrings

Based on the advantages of utf8, many string operations do not require decoding, and you can directly determine whether a string isPrefix, suffix or substring (byte-byte comparison method is used when judging)

package main
import (
	"fmt"
	"strings"
)
func main() {
str := "abcd"
	s := "abcd"
	//Prefix	if len(s) <= len(str) && str[:len(s)] == s {
		("Is PreFix!")
	}
	//suffix	if len(s) <= len(str) && str[len(str)-len(s):] == s {
		("Is SufFix!")
	}
	//Substring	if (str, s) {
		("Is Contanins!")
	}
}
// Output// Is PreFix!
// Is SufFix!
// Is Contanins!

3. Traversal of strings

range:This method traverses the number of characters in the string

package main
import (
	"fmt"
)
func main() {
	str := "abcd love lotus said"
	for i, v := range str {
		("str[%d] is %c\n", i, v)
	}
}
// Output// str[0] is a
// str[1] is b
// str[2] is c
// str[3] is d
// str[4] is love// str[7] is lotus// str[10] is explain

4. Conversion of string and byte slice

Utilize[ ]byteData types implement operations on strings, using []byte to edit characters first and then convert them to string types.

//Not editables := "abcdef"
//Editableb := []byte(s)
str = string(b)

tool:This tool can continuously write new characters and other byte data

func AddByte(str string) string {
	var buf 
	for _, v := range str {
		//Add original characters		(v)
		//Insert space		(" ")
	}
	return ()
}

This is the article about the principle of Golang string type and its usage method. For more related Golang string type content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!