面向对象编程中,尽量对函数进行封装,对于没有函数变量的对象,使用static关键字尤其方便。
go中没有static关键字,必须每次new一个出来。
type Handler struct {
}
func NewHandler() *Handler {
h := &Handler{}
return h
}
func (*Handler) Method_1 (){
}
func (*Handler) Method_2 () {
}
go语言里面的for循环比较灵活
func main(){
for {
fmt.Printf("while true\n")
break
}
for i:=0; i < 5; i = i + 1 {
fmt.Printf("while data:%d\n", i)
}
i := 0
for i < 5 {
fmt.Printf("while ok:%d\n", i)
i = i + 1
}
}
go中每个协程都必须捕获panic, 否则任何一个协程问题,都会导致整个程序dump掉。
func no_panic_get(a, b int){
/* //每个go协程都必须捕获panic, 否则整个程序都会panic
defer func() {
if r := recover(); r != nil {
fmt.Printf("no_panic_get, err:%v\n", r)
}
}()
*/
c := a / b
fmt.Printf("no_panic_get res:%v\n", c)
}
func main(){
defer func() {
if r := recover(); r != nil {
fmt.Printf("exec failed, err:%v\n", r)
} else {
fmt.Printf("exec success\n")
}
}()
go no_panic_get(1,0) //no_panic_get没有捕获panic, 程序直接panic崩溃
time.Sleep(time.Duration(5 * time.Second))
}
defer函数,是在函数最后执行的。
属于先申明,后执行原则;多个defer,申明时入栈,出栈时执行,也就是逆序执行。
只要第一个申明的defer,作为最后一个捕获panic程序执行就可以。
func main(){
defer func() {
fmt.Printf("defer func1\n")
} ()
defer func() {
fmt.Printf("defer func2\n")
} ()
fmt.Printf("main func\n")
}
interface类似C/C++中的void*类型,可以将任何类型转为interface类型。
不同的是, interface可以确定保存的是那种类型的数据. 使用 v.(type)
type A struct {
age int
sex string
}
type B struct {
peer []string
name string
}
func main(){
aData := &A{age:19}
bData := &B{[]string{"zhangsan", "lisi"},"wang"}
ctx := make(map[string]interface{}) //容器
ctx["1"] = aData
ctx["2"] = bData
for _, v := range ctx {
switch v.(type) {
case *A:
fmt.Printf("type A %v\n", v)
case *B:
fmt.Printf("type B %v\n", v)
default:
fmt.Printf("type unkown\n")
}
}
}
go中大小写区分访问权限, 在定义函数,定义变量时需要注意。
小写变量,不能被json序列化。
小写函数,外部不能访问。
go1.4 之前版本中, map[int]string, 因为key是int不能正确序列号。
type Stu struct {
Age int
Name string
}
func main(){
ctx := make(map[int]*Stu)
ctx[1] = &Stu{12,"li"} //按照定义顺序初始化
ctx[2] = &Stu{ Age:18} //初始化部分字段; 其他字段默认值
jsonres, _ := json.Marshal(ctx)
result := string(jsonres)
fmt.Printf("%s\n", result)
newctx := make(map[int]*Stu)
if err := json.Unmarshal([]byte(result), &newctx); err == nil { //第二个参数需要是一个指针
for k, v := range newctx{
fmt.Printf("k:%v val:%v\n", k, v)
}
}
}
在go语言中,任何类型在声明后没有赋值的情况下,都对应一个零值。
go语言函数是支持多返回值的,因此在写go函数时,都会带一个error类型返回值表示是否有错误。
可以使用fmt构造一个error返回。
func getError() error {
a := 1
b := 0
return fmt.Errorf("a/b is not vaild, a:%d b:%d", a, b)
}
func main(){
fmt.Printf("err[%v]", getError())
}
原文:https://www.cnblogs.com/xudong-bupt/p/10657882.html