zoukankan      html  css  js  c++  java
  • Golang使用注意点

    select break

    go中使用for select 结构,select的break只能跳出break,不能跳出for循环

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    	ch := make(chan int)
    	ok := make(chan bool)
    	go func() {
    		ch <- 1
    		ch <- 2
    		ch <- 3
    		close(ch)
    	}()
    	go test(ch, ok)
    	<-ok
    }
    
    func test(ch chan int, ook chan bool) {
    	for {
    		select {
    		case resp,ok := <-ch:
    			if !ok {
    				break
    			}
    			fmt.Println(resp)
    		case <-time.After(time.Second):
    			fmt.Printf("1s now!")
    			break
    		}
    	}
    }
    

    chan close

    向关闭的chan读取数据,chan会返回相关的空数据,内置类型返回0值数据,自定义struct返回空指针

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    type Obj struct {
    	val  int
    	name string
    }
    
    func main() {
    	ch := make(chan int, 1)
    	ok := make(chan *Obj, 2)
    	go func() {
    		defer close(ok)
    		defer close(ch)
    		ok <- &Obj{val: 1, name: "ww"}
    		ch <- 1
    	}()
    	go test(ch, ok)
    	select {
    	case <-time.After(2 * time.Second):
    		fmt.Println("2s close main")
    	}
    }
    
    func test(ch chan int, ok chan *Obj) {
    	i := 0
    	for {
    		o := <-ok
    		fmt.Println(o)
    		r := <-ch
    		fmt.Println(r)
    		i++
    		if i >= 2 {
    			break
    		}
    	}
    }
    

    go panic

    协程panic,如果没有recover,则会crash整个程序

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main()  {
    	go func() {
    		panic("1")
    	}()
    	time.Sleep(time.Second)
    	fmt.Println("2")
    }
    
  • 相关阅读:
    Linux进程关系
    ambari 卸载脚本
    CentOS-7.2安装Ambari-2.6.1
    MYSQL57密码策略修改
    CentOS7 离线安装MySQL
    centos 安装mysql Package: akonadi-mysql-1.9.2-4.el7.x86_64 (@anaconda)
    mysql 数据备份
    spring-boot-starter-thymeleaf对没有结束符的HTML5标签解析出错
    ssh: scp命令
    python:os.path
  • 原文地址:https://www.cnblogs.com/weiweng/p/13159595.html
Copyright © 2011-2022 走看看