Go:
Init Function:
func init() {
initialize
}
init可以有多个,init函数无参数,无返回值
Main Function:
func main() {
operf
}
Functions Signature:
func operf(params types) {
operf
}
func funcf(params types) returnType {
operf
return value
}
varname:=funcf(args)
func funcf(params types) (returnType sequence) {
operf
return value sequence
}
var sequence:=funcf(args)
var funcf=func(params types) (returnType sequence) {
operf
return value sequence
}
funcf:=func(params types) (returnType sequence) {
operf
return value sequence
}
fn:=funcf(args)
func funcf(params types) func(paramtypes) (returnTypes) {
operf1
return func(params types) (returnType sequence) {
operf2
return values
}
}
fn1:=funcf(args)
fn2:=fn1(args)
func outer(params types,func inner(types) (returnTypes)) (returnTypes) {
operf
fn:=inner(args)
return values
}
fn:=outer(args,funcf)
Function Body:
Base types:
int,int8,int16,int32,int64,
uint,uint8,uint16,uint32,uint64,不能表示负数
float32,float64
complex64,complex128
bool,byte, string,nil,
rune,uintptr
Declare variables:
var varname type=value
varname:=value
Declare constants:
const Const=value
if:
if initial;condf {
operf1
} else if condf {
operf2
} else {
operf3
}
if condf {
operf1
} else {
operf2
}
for:
for loopvar:=initial;loopcondf(loopvar);loopoperf(loopvar) {
operf
}
for index,loopvar:=ranges {
operf
}
for index,value:=range funcf(params types) {
operf
}
for key,value:=range mappings {
operf
}
for index,item:=range slices {
operf
}
for index,char:=range string {
operf
}
for index:=range slices {
operf
}
for index:=range string {
operf
}
for initial;condf {
operf
}
for {
operf
}
Switch:
switch initial;expr {
case value sequence:
operf1
case value sequence:
operf2
default:
operfn
}
switch {
case condf1:
operf1
case condf2:
operf2
default:
operfn
}
switch initial;expr {
case value sequence:
operf1
fallthrough
case value sequence:
operf2
fallthrough
default:
operfn
}
只有在 case 中明确添加了 fallthrough 关键字,才会继续执行紧跟的下一个 case
Goroutine:
go func(params types) {
operf
}(args)
自执行
Defer:
defer func(args)
defer func(params types) (returnTypes){
operf
return values
}(args)
自执行
先入后出,先defer的先被压入函数栈,后取出执行
defer fmt.Println(args)
Channel:
channel:=make(chan type,buffer)
Channel Type:
chan type (two-way channel)
composite types:
<-chan type (read-out channel)
chan<- type (write-in channel)
Types:
base types
slice
map
array
struct{}
interface{}
func (types) types
NewType
Write:
channel<-value
value可以为函数
Read:
<-channel (pop a value from channel)
variable:=<-channel (receive a value from channel)
Close:
close(channel)
Input:
Single Line:
bytes,ok:=fmt.Scanf(&variable sequence)
Multi Lines:
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Please enter multiple lines of input, press Ctrl+D when done:")
var lines []string
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
Output:
import "fmt"
fmt.Println(value sequence)
fmt.Print(value sequence)
fmt.Printf(formatstring,value sequence)
Strings:
var str string=value
str:=value
str3:=str1+str2
char:=str[index]
str:=`
keep formatted string
`
Arrays:
var arr [size]type
arr:=[size]type{item sequence}
Matrixs:
var mat [size1][size2]type
mat:=[size1][size2]type{{items} sequence}
Slices:
var slice []type
slice:=make([]type,size)
slice:=[]type{item sequence}
slice:=arr[begin:end]
Mappings:
map:=make(map[keyType]valueType)
map[key]=value
value,ok=map[key]
delete(map,key)
Ranges:
无
Errors:
var err error=errors.New(prompt)
OIP:面向接口编程
Types:
type NewType type
Interfaces:
type interf interface {
func method(prarms types) (returnType)
}
Structs:
type Class struct {
property type
}
Methods:
func (obj Class) method(params types) (returnType sequence) {
operf
return values
}
struct.method(args)
just like this:
func method(obj Class,params types) (returnType sequence) {
operf
return values
}
func (ptr *Class) method(params types) (returnType sequence) {
operf
return values
}
&struct.method(args)
模版:
main:主函数
imports:
init:init函数
p:package
vars: var (
var=value
)
consts:const (
const=value
)
pl:fmt.Println()
pf:fmt.Printf("\n",)
fmt:
import "fmt"
func _() {
fmt.Print("")
}