Part5

    技术2022-07-10  156

    示例:

    package main import ( "fmt" ) func main() { // 重点1的示例。 var srcInt = int16(-255) // 请注意,之所以要执行uint16(srcInt),是因为只有这样才能得到全二进制的表示。 // 例如,fmt.Printf("%b", srcInt)将打印出"-11111111",后者是负数符号再加上srcInt的绝对值的补码。 // 而fmt.Printf("%b", uint16(srcInt))才会打印出srcInt原值的补码"1111111100000001"。 fmt.Printf("The complement of srcInt: %b (%b)\n", uint16(srcInt), srcInt) dstInt := int8(srcInt) fmt.Printf("The complement of dstInt: %b (%b)\n", uint8(dstInt), dstInt) fmt.Printf("The value of dstInt: %d\n", dstInt) fmt.Println() // 重点2的示例。 fmt.Printf("The Replacement Character: %s\n", string(-1)) fmt.Printf("The Unicode codepoint of Replacement Character: %U\n", '�') fmt.Println() // 重点3的示例。 srcStr := "你好" fmt.Printf("The string: %q\n", srcStr) fmt.Printf("The hex of %q: %x\n", srcStr, srcStr) fmt.Printf("The byte slice of %q: % x\n", srcStr, []byte(srcStr)) fmt.Printf("The string: %q\n", string([]byte{'\xe4', '\xbd', '\xa0', '\xe5', '\xa5', '\xbd'})) fmt.Printf("The rune slice of %q: %U\n", srcStr, []rune(srcStr)) fmt.Printf("The string: %q\n", string([]rune{'\u4F60', '\u597D'})) } package main import "fmt" func main() { // 示例1。 { type MyString = string str := "BCD" myStr1 := MyString(str) myStr2 := MyString("A" + str) fmt.Printf("%T(%q) == %T(%q): %v\n", str, str, myStr1, myStr1, str == myStr1) fmt.Printf("%T(%q) > %T(%q): %v\n", str, str, myStr2, myStr2, str > myStr2) fmt.Printf("Type %T is the same as type %T.\n", myStr1, str) strs := []string{"E", "F", "G"} myStrs := []MyString(strs) fmt.Printf("A value of type []MyString: %T(%q)\n", myStrs, myStrs) fmt.Printf("Type %T is the same as type %T.\n", myStrs, strs) fmt.Println() } // 示例2。 { type MyString string str := "BCD" myStr1 := MyString(str) myStr2 := MyString("A" + str) _ = myStr2 //fmt.Printf("%T(%q) == %T(%q): %v\n", // str, str, myStr1, myStr1, str == myStr1) // 这里的判等不合法,会引发编译错误。 //fmt.Printf("%T(%q) > %T(%q): %v\n", // str, str, myStr2, myStr2, str > myStr2) // 这里的比较不合法,会引发编译错误。 fmt.Printf("Type %T is different from type %T.\n", myStr1, str) strs := []string{"E", "F", "G"} var myStrs []MyString //myStrs := []MyString(strs) // 这里的类型转换不合法,会引发编译错误。 //fmt.Printf("A value of type []MyString: %T(%q)\n", // myStrs, myStrs) fmt.Printf("Type %T is different from type %T.\n", myStrs, strs) fmt.Println() } // 示例3。 { type MyString1 = string type MyString2 string str := "BCD" myStr1 := MyString1(str) myStr2 := MyString2(str) myStr1 = MyString1(myStr2) myStr2 = MyString2(myStr1) myStr1 = str //myStr2 = str // 这里的赋值不合法,会引发编译错误。 //myStr1 = myStr2 // 这里的赋值不合法,会引发编译错误。 //myStr2 = myStr1 // 这里的赋值不合法,会引发编译错误。 } }

    go语言的模块的嵌套和查找规则

    1.在自己的代码块内查找 2.从包含自己代码块的父代码块中查找 3.一直到当前的代码包代表的代码块中 Note:如果import . package 用这种引用方式,那么就相当于把package下面的函数视为当前的包下面的代码,也需要查找。

    思考题

    如果我们在go语言查找标识符范围的时候,用了import . XXXX这种导入方式,如果xxxx和我们的代码包中有变量重复,那么GO会把它当作可重名变量还是冲突?

    如果导入的包里面的变量是私有的,也就是小写开头的,那么就不存在重复,但如果是public的,而且是全局的变量那么就会有冲突。但如果是在子模块内,那就不会冲突了。 package main import . "fmt" var container = []string{"ZERO", "ONE", "TWO"} func main() { container := map[int]string{0: "zero", 1: "one", 2: "two"} Println(container[0], container[1], container[2]) }
    Processed: 0.009, SQL: 9