1, variable definition of several ways such as:
//方式一:普通定义 var a int = 1 fmt.Println("a = ", a) //方式二:类型推导 var b = 10 fmt.Println("b = ", b) //方式三:省略var name := "tom" fmt.Println("name = ", name) //多变量声明 var n1, n2, n3 int fmt.Println("n1 = ",n1 ,"n2 = ", n2,"n3 = ",n3) var n4, name1, n5 = 100, "tom", 888 fmt.Println("n4 = ",n4 ,"name1 = ", name1,"n5 = ",n5) n6, name2, n7 := 100, "tom", 888 fmt.Println("n6 = ",n6 ,"name2 = ", name2,"n7 = ",n7)
2, the Go language definition of variables or the introduction of the package is not used, can not be compiled by using the Ignore retention definitions and packages introduced such as:
package main import ( "fmt" _ "bytes" //忽略bytes包 ) func main () { var num1 int = 10 _ = num1 //忽略变量 fmt.Println("hello world") }
3, automatic collation of formatting code instructions
//自动整理格式化输出linux~# gofmt hello.go package mainimport "fmt"func main() { fmt.Println("hello world")}//将整理格式化后的类容保存文件linux~# gofmt -w hello.go
4. Curly braces cannot be wrapped
//正确的func main () { fmt.Println("hello world")}//错误的func main (){ fmt.Println("hello world")}
5. Return and receive of variable multiple values
package mainimport ( "fmt" )func main () { var num1 int = 10 var num2 int = 1 //接收两个值 sum , sub := getSumAndSub(num1, num2) fmt.Println("sum =",sum, "sub =", sub) //只接收一个值 sum0 , _ := getSumAndSub(num1, num2) fmt.Println("sum =",sum0)}func getSumAndSub(num1 int, num2 int) (int, int) { sum := num1 + num2 sub := num1 - num2 return sum, sub}
6, go no character type (char), generally expressed in byte type, and go encoding is unified use UTF8, each character will have a fixed code value
//go没有字符类型 var c1 byte = 'a' var c2 byte = '1' //输出的都是码值 fmt.Println("c1", c1, "c2", c2) //输出对应的字符 fmt.Printf("c1=%c c2=%c", c1, c2) //汉字 var c3 byte = '被'//溢出 fmt.Printf("c3=%c c3码值=%d", c3, c3) var c4 int = '被' fmt.Printf("c4=%c c4码值=%d", c4, c4) //输出码值对应的字符 var c5 int = 22269 fmt.Printf("c5=%c", c5)
7. Strings (string) once assigned cannot be modified by subscript
var str2 = "hello" //str2[0] = 'a' //报错
8. Output in string native mode
var str string = `每个生命的方式是自然界一种力的方式。有些人的生命像沉静的湖,有些像白云飘荡的一望无际的天空,有些像丰腴富饶的平原,有些像断断续续的山峰。我觉得约翰·克利斯朵夫的生命像一条河。 ——罗曼.罗兰《约翰·克利斯朵夫》` fmt.Println(str)
9, string + number stitching
//加号放在上一行末尾 str4 := "hello" + "world" fmt.Println("str4 = ", str4)
10. Conversion between different types of data in Java and C requires casting (display conversion)
var i int32 = 10 var f1 float32 = float32(i) fmt.Println("f1 = ", f1) var n1 int32 = 1 var n2 int8 var n3 int8 n2 = int8(n1) + 127 //编译能过,但结果溢出 //n3 = int(n1) + 128 //n3是int8 报错 fmt.Println("n1 =", n1, "n2 =", n2," n3 =",n3)
11. Conversion between string type and other basic types
Other basic data types go to String
//第一种方式 fmt.Sprintf str = fmt.Sprintf("%d", num1) fmt.Printf("str type %T str=%v\n", str, str) str = fmt.Sprintf("%f", num2) fmt.Printf("str type %T str=%v\n", str, str) str = fmt.Sprintf("%t", b) fmt.Printf("str type %T str=%v\n", str, str) str = fmt.Sprintf("%c", ch) fmt.Printf("str type %T str=%v\n", str, str) //第二种方式 strconv str = strconv.FormatInt(int64(num1), 10) fmt.Printf("str type %T str=%v\n", str, str)
String type go to other base data types
var str1 string = "true" var b bool b, _ = strconv.ParseBool(str1) fmt.Printf("b type %T b=%v\n", b, b) var str2 string = "123456" var n1 int64 n1, _ = strconv.ParseInt(str2, 10, 64) fmt.Printf("n1 type %T n1=%v\n", n1, n1) var str3 string = "123dd456" var n2 int64 n2, _ = strconv.ParseInt(str3, 10, 64) fmt.Printf("n1 type %T n1=%v\n", n2, n2) var str4 string = "123" var n3 int64 n3, _ = strconv.ParseInt(str4, 10, 64) n5 := int(n3) fmt.Printf("n3 type %T n1=%v\n", n3, n3) fmt.Printf("n5 type %T n1=%v\n", n5, n5) var str5 string = "123.456" var n4 float64 n4, _ = strconv.ParseFloat(str5, 64) fmt.Printf("n1 type %T n1=%v\n", n4, n4)
12, in-package variable and function names, and constant names, capitalized as public methods (equivalent to other languages) other packages can be used, the first letter lowercase is private method (equivalent to other languages private) other packages can not be used
var HeroName string = "张飞" //公共 publicvar ambitiousName string = "曹操" //私有 private
13, only i++ and I--, and can only be used alone can not be assigned value
//++ -- 只能独立使用 var i = 8 i++ fmt.Println("i =",i) i-- fmt.Println("i =",i) //错误使用 ++i //错误 --i //错误 var j int; j = i++ //错误 if i++ > 10 { //错误 return }