规范
合法标识符举例
非法标识符举例
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
append bool byte cap close complex complex64 complex128 uint16
copy false float32 float64 imag int int8 int16 uint32
int32 int64 iota len make new nil panic uint64
print println real recover string true uint uint8 uintptr
package main
import "fmt"
func main() {
var a = 21
var b = 10
var c int
fmt.Printf("[初始化的值是什么: a=%d b=%d c=%d]\n", a, b, c)
// 加法
c = a + b
fmt.Printf("[加法操作: a+b=%d]\n", c)
// 减法
c = a - b
fmt.Printf("[减法操作: a-b=%d]\n", c)
// 乘法
c = a * b
fmt.Printf("[乘法操作: a*b=%d]\n", c)
// 减法
c = a / b
fmt.Printf("[除法操作: a/b=%d]\n", c)
// 取余
c = a % b
fmt.Printf("[减法操作: a对b取余=%d]\n", c)
// 自增
c++
fmt.Printf("[c自增: c++=%d]\n", c)
// 自减
c--
fmt.Printf("[c自减: c++=%d]\n", c)
/*
[初始化的值是什么: a=21 b=10 c=0]
[加法操作: a+b=31]
[减法操作: a-b=11]
[乘法操作: a*b=210]
[除法操作: a/b=2]
[减法操作: a对b取余=1]
[c自增: c++=2]
[c自减: c++=1]
*/
}
> < == !=
package main
import "fmt"
func main() {
var a = 10
var b = 5
if a > b {
fmt.Printf("a>b")
}
}
package main
import "fmt"
func main() {
var a = true
var b = false
if a && b {
fmt.Printf("[ab 同为true才为true]\n")
}
if a || b {
fmt.Printf("[a b 中有一个为true 就是true]\n")
}
if !(a && b) {
fmt.Printf("[a b同为true再取非为true]\n")
}
}
package main
import "fmt"
func main() {
var a uint = 60
var b uint = 13
var c uint = 0
fmt.Printf("[a=%d 二进制=%08b]\n", a, a)
fmt.Printf("[b=%d 二进制=%08b]\n", b, b)
c = a & b
fmt.Printf("[与操作][%d=%d&%d][%08b=%08b & %08b ]\n", c, a, b, c, a, b)
c = a | b
fmt.Printf("[或操作][%d=%d | %d][%08b=%08b | %08b ]\n", c, a, b, c, a, b)
c = a ^ b
fmt.Printf("[异或操作][%d=%d^%d][%08b=%08b ^ %08b ]\n", c, a, b, c, a, b)
c = a << b
fmt.Printf("[左移放大操作][%d=%d&%d][%08b=%08b & %08b ]\n", c, a, b, c, a, b)
c = a >> b
fmt.Printf("[右移动缩小操作][%d=%d&%d][%08b=%08b & %08b ]\n", c, a, b, c, a, b)
/*
[a=60 二进制=00111100]
[b=13 二进制=00001101]
[与操作][12=60&13][00001100=00111100 & 00001101 ]
[或操作][61=60 | 13][00111101=00111100 | 00001101 ]
[异或操作][49=60^13][00110001=00111100 ^ 00001101 ]
[左移放大操作][491520=60&13][1111000000000000000=00111100 & 00001101 ]
[右移动缩小操作][0=60&13][00000000=00111100 & 00001101 ]
*/
}
+= -= *= ...
package main
import "fmt"
func main() {
var a = 21
var c int
c = a
fmt.Println(c, a)
c += a
fmt.Println(c, a)
c *= a
fmt.Println(c, a)
/*
21 21
42 21
882 21
*/
}
var a *int
package main
import "fmt"
func main() {
var a = 4
var ptr *int
fmt.Printf("[a 变量类型为 %T]\n", a)
fmt.Printf("[ptr 变量类型为 %T]\n", ptr)
ptr = &a
fmt.Printf("[a 变量的值为 %d]\n", a)
fmt.Printf("[*ptr 的值为%d ]\n", *ptr)
fmt.Printf("[ptr 的值为%v ]\n", ptr)
fmt.Printf("[a 的指针地址为%p ]\n", &a)
/*
[a 变量类型为 int]
[ptr 变量类型为 *int]
[a 变量的值为 4]
[*ptr 的值为4 ]
[ptr 的值为0xc00001a098 ]
[a 的指针地址为0xc00001a098 ]
*/
}
var p int
var a,b,c,d string
var p int = 10
p:=10
const Pi = 3.1415
const Max float64 = 1000.11111
const cname = "go编程"
const dname
package main
import "fmt"
const (
a = iota
b
c
d
)
func main() {
fmt.Println(a, b, c, d)
}
package main
import "fmt"
type MyType int
const (
T1 MyType = iota
T2
T3
T4
)
func main() {
fmt.Println(T1, T2, T3, T4)
}
package main
import "fmt"
type MyType int
const (
T1 MyType = iota
T2
_
_
T3
T4
)
func main() {
fmt.Println(T1, T2, T3, T4) //0 1 4 5
}
package main
import "fmt"
type MyType int
const (
a = iota
b = 5
c
d = iota
e
)
func main() {
fmt.Println(a, b, c, d, e) //0 5 5 3 4
}
package main
import "fmt"
type MyType int
const (
T1 MyType = 1 << iota
T2
_
_
T3
T4
)
func main() {
fmt.Println(T1, T2, T3, T4) //1 2 16 32
}
package main
import "fmt"
type MyType int
const (
_ = iota
KB float64 = 1 << (10 * iota)
MB
GB
TB
)
func main() {
fmt.Println(KB, MB, GB, TB) //1024 1.048576e+06 1.073741824e+09 1.099511627776e+12
fmt.Println(1 << 10)
}
package main
import "fmt"
func T1() string {
str1 := "函数T1中的字符串变量"
fmt.Println(str1)
return str1
}
func main() {
// .\compute.go:12:14: undefined: str1
// 试图引用在函数里面定义的变量
//fmt.Println(str1)
for i := 0; i < 10; i++ {
fmt.Println(i)
}
// 报错是这个.\compute.go:18:14: undefined: i
// 试图引用在for里面定义的变量
//fmt.Println(i)
if str := T1(); str == "" {
fmt.Println("[函数返回为空]")
}
//
// 报错是这个.\compute.go:26:14: undefined: str
// 试图引用在if里面定义的变量
//fmt.Println(str)
}
原文:https://www.cnblogs.com/zhanghan5/p/15004967.html