javer to go introductory notes - string

abstract

This paper introduces the characteristics of string in Go language, compares it with string in java, and finally introduces the common conversion methods between string and other basic types in Go.

1. go string overview

definition

String is also defined as the basic data type in go. The type keyword is string. Its essence is an array of characters that cannot be modified. There are two common definitions:

var str string = "hello"
str := "hello"
essence

The essence of string type in go language is similar to a slice pointing to character array. The size of this type is fixed at 16 bytes, of which 8 bytes represent a pointer to character array and the other 8 bytes represent the length of string. The following can be verified:

var str1 string = "hello"
    fmt.Printf("str1 The type of is%T,\"%s\"Occupied by variable itself%d Bytes, string length%d\n", str1, str1, unsafe.Sizeof(str1), len(str1))
    str2 := "hello Zada"
    fmt.Printf("str2 The type of is%T,\"%s\"Occupied by variable itself%d Bytes, string length%d\n", str2, str2, unsafe.Sizeof(str2), len(str2))

******result*******
str1 The type of is string,"hello"The length of the variable itself is 16 bytes, and the length of the variable itself is 5 bytes 
str2 The type of is string,"hello Zada"The variable itself occupies 16 bytes and the string length is 11

2. Similarities and differences between String and java

Differences
  • 1. In java, string is an object, while in go, string is a basic data type
  • 2. String objects in java cannot directly index each character by array, but golang can, such as:
str := "123"
fmt.Println(str[1])

*****output*****
50  //The character byte in go is output by unsigned shaping by default
Similarities
  • Strings in java and go are immutable
    • The immutability of java is reflected in that users cannot directly modify the object itself through object reference, but the methods provided by jdk, such as subString(), will return new objects and will not modify the object itself.
    • The immutability of go is reflected in that it does not allow users to modify the string array it points to through array index, so as to ensure immutability.

3. Precautions for use

  • 1. Strings in go are similar to slices, so more slices can be derived from them, but all slices cannot modify the character array, such as the case:
var str string = "hello"
var strSlice = str[2:]
fmt.Println(strSlice[1:])

********output*******
lo
  • 2. There are two representations of strings in go:
    • This is as like as two peas "java". This is the same as that of the Chinese character.
    • Marked with reverse quotation marks, it does not recognize escape characters. It can be used for source code display, etc. at the same time, it can also directly recognize carriage return

4. Conversion between string and other basic types

Basic data type to string
  • 1. Through FMT For the conversion of sprint() function, you can find the explanation of the function through the official document%v: convert according to the original format of the variable http://www.golang.ltd/
  • 2. Through the function conversion of FormatInt and ForMatFloat under strconv package, go language does not support overloading. FormatInt is only used for int64, which is a little troublesome and does not support character conversion
//Mode 1
intNum := 25
var byteNum rune = 'c'
floatNum := 3.14159
fmt.Println(fmt.Sprintf("intNum The string of is%v", intNum))
fmt.Println(fmt.Sprintf("byteNum The string of is%c", byteNum))
fmt.Println(fmt.Sprintf("floatNum The string of is%.3f", floatNum))
//Mode 2
fmt.Println("intNum The string of is", strconv.FormatInt(int64(intNum), 10))
fmt.Println("floatNum The string of is", strconv.FormatFloat(floatNum, 'f', -1, 64))
string to basic data type
  • It is implemented by ParseInt, ParseFloat and other functions under strconv package. If it cannot be converted, it will directly return the default value
str5 := "true"
var num5 bool
num5, _ = strconv.ParseBool(str5)
fmt.Printf(str5+"The type of representative is%T\n", num5)

str6 := "hello" //If it cannot be converted, the default value is returned
var num6 float64
num6, _ = strconv.ParseFloat(str6, 64)
fmt.Println(num6)

********output*********
true The type of representative is bool 
0

5. Test code and output

code
package main
import (
    "fmt"
    "strconv"
    "unsafe"
)
func main() {
    // 1. string is defined as the basic data type in go. It is a character array. In essence, it is an array that cannot be modified
    fmt.Println("**************************************************************************************************************")
    fmt.Println("1,string stay go Is defined as a basic data type in. In fact, it is a character array. Its essence is similar to slicing. It points to an array through a pointer. The difference is that the array cannot be modified")
    fmt.Println("Because the string variable itself does not represent the length of the string, its variable is fixed to occupy 16 bytes, and its data structure should be a pointer to the character array, and then a int64 Represents the length")
    fmt.Println("")
    var str1 string = "hello"
    fmt.Printf("str1 The type of is%T,\"%s\"Occupied by variable itself%d Bytes, string length%d\n", str1, str1, unsafe.Sizeof(str1), len(str1))
    str2 := "hello Zada"
    fmt.Printf("str2 The type of is%T,\"%s\"Occupied by variable itself%d Bytes, string length%d\n", str2, str2, unsafe.Sizeof(str2), len(str2))
    var strSlice = str2[4:]
    fmt.Println(strSlice[1:])
    fmt.Println("")
    // 2. string is immutable in go, which is consistent with java
    // The immutability of string in java is reflected in that the string object will not change, and only new objects will be returned through the subString method of string
    // The immutability of string in go is reflected in the fact that the string cannot be modified through the assignment of character array
    fmt.Println("**************************************************************************************************************")
    fmt.Println("2,string stay go Is immutable, which is different from java agreement")
    fmt.Println("java in string Immutability is reflected in that the string object will not change through string of subString Methods and so on will only return new objects")
    fmt.Println("go in string Immutability is reflected in the fact that the string cannot be modified through the assignment of character array")
    fmt.Println("")
    str3 := str1
    str3 = str2
    // str3[0] = 'c';
    fmt.Println("str3=", str3)
    fmt.Println("")
    // 3. Two expressions of string
    // This is as like as two peas "java". This is the same as that of the Chinese character.
    // Marked with reverse quotation marks, it does not recognize escape characters. It can be used for source code display, etc. at the same time, it can also directly recognize carriage return
    fmt.Println("**************************************************************************************************************")
    fmt.Println("3,Two expressions of string")
    fmt.Println("Through double quotation marks\"\"Annotation, this and java As like as two peas, you can recognize escaped characters.")
    fmt.Println("Through backquotes``Annotation, do not recognize escape characters, can be used for source code display, etc., and can also directly recognize carriage return")
    fmt.Println("")
    fmt.Printf("str4=\n")
    fmt.Println(`str4=\n
    `)
    fmt.Println("")
    // 4. Conversion between string and other basic types
    // Basic data type to string
    // 1. Through FMT For the conversion of sprint() function, you can find the explanation of the function through the official document%v: convert according to the original format of the variable http://www.golang.ltd/
    // 2. Through the function conversion of FormatInt and ForMatFloat under strconv package, go language does not support overloading. FormatInt is only used for int64, which is a little troublesome and does not support character conversion
    // string to basic data type} is realized by ParseInt, ParseFloat and other functions under strconv package. If it cannot be converted, it will directly return the default value
    fmt.Println("**************************************************************************************************************")
    fmt.Println("4,string Conversion with other basic types")
    fmt.Println("Basic data type conversion string")
    fmt.Println("1,adopt fmt.Sprint()Function conversion, you can find the explanation of the function through the official documents %V:Convert according to the original format of the variable http://www.golang.ltd/")
    fmt.Println("2,adopt strconv Under the bag FormatInt,ForMatFloat Equal function conversion, go Language does not support overloading, FormatInt Only right int64 It is a little troublesome to use, and does not support character conversion")
    fmt.Println("string Transfer to basic data type  adopt strconv Under the bag ParseInt,ParseFloat If it cannot be converted, the default value will be returned directly")
    fmt.Println("")
    intNum := 25
    var byteNum rune = 'c'
    floatNum := 3.14159
    fmt.Println(fmt.Sprintf("intNum The string of is%v", intNum))
    fmt.Println(fmt.Sprintf("byteNum The string of is%c", byteNum))
    fmt.Println(fmt.Sprintf("floatNum The string of is%.3f", floatNum))
    fmt.Println("intNum The string of is", strconv.FormatInt(int64(intNum), 10))
    fmt.Println("floatNum The string of is", strconv.FormatFloat(floatNum, 'f', -1, 64))
    str5 := "true"
    var num5 bool
    num5, _ = strconv.ParseBool(str5)
    fmt.Printf(str5+"The type of representative is%T\n", num5)
    str6 := "hello" //If it cannot be converted, the default value is returned
    var num6 float64
    num6, _ = strconv.ParseFloat(str6, 64)
    fmt.Println(num6)
}
output

1. string is defined as the basic data type in go. In fact, it is a character array. Its essence is similar to slicing. It points to an array through a pointer. The difference is that the array cannot be modified
Because the string variable itself does not represent the length of the string, its variable accounts for 16 bytes. Its data structure should be a pointer to the character array, and then an int64 represents the length
The type of str1 is string. The "hello" variable itself takes 16 bytes and the string length is 5
The type of str2 is string. The "Hello scatter" variable itself occupies 16 bytes, and the length of the string is 11
Zada

2. string is immutable in go, which is consistent with java
The immutability of string in java is reflected in that the string object will not change, and only new objects will be returned through the subString method of string
The immutability of string in go is reflected in the fact that the string cannot be modified through the assignment of character array

str3= hello scatter

3. Two expressions of string
This is as like as two peas "java". This is the same as that of the Chinese character.
Marked with reverse quotation marks, it does not recognize escape characters. It can be used for source code display, etc. at the same time, it can also directly recognize carriage return

str4=
str4=\n

4. Conversion between string and other basic types
Basic data type to string
1. Through FMT Sprint() function conversion, you can find the explanation of the function through the official document% V: convert according to the original format of the variable http://www.golang.ltd/
2. Through the function conversion of FormatInt and ForMatFloat under strconv package, go language does not support overloading. FormatInt is only used for int64, which is a little troublesome and does not support character conversion
string to basic data type is realized by ParseInt, ParseFloat and other functions under strconv package. If it cannot be converted, it will directly return the default value

The string of intNum is 25
The string of byteNum is c
The string of floatNum is 3.142
The string of intNum is 25
The string of floatNum is 3.14159
The type represented by true is bool
0

Tags: Go

Posted by jayjay960 on Mon, 18 Apr 2022 21:20:20 +0930