哥伦比亚大学的声明

数组、映射的范围


func main(){
    l := []string{"python", "go", "java"}
    for i := 0; i < len(l); i++{
        fmt.Println(i, l[i])
    }

    for i, v := range l{
        fmt.Println(i, v)
    }

    for _, v := range l{
        fmt.Println(v)
    }

    m := map[string]int{"apple": 100, "banana": 200,}

    for k, v := range m{
        fmt.Println(k, v)
    }

    for k := range m{
        fmt.Println(k)
    }

    for _, v := range m{
        fmt.Println(v)
    }
}

创建和写入日志文件


func LoggingSettings(logFile string){
    logfile, _ := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    multilogFile := io.MultiWriter(os.Stdout, logfile)
    log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
    log.SetOutput(multilogFile)
}


func main(){
    LoggingSettings("test.log")
    _, err := os.Open("abc.log")
    if err != nil{
        log.Fatalln("abc.logなんてないでほんまに")
    }
}

惊慌

func main(){
    file, err := os.Open("./lesson.go")
    if err != nil {
        log.Fatalln("Error")
    }
    defer file.Close()
    data := make([]byte, 100)
    count, err := file.Read(data)
    if err != nil {
        log.Fatalln("Error")
    }
    fmt.Println(count, string(data))

    err = os.Chdir("hello")
    if err != nil{
        log.Fatalln(err)
    } else {
        file, err := os.Open("./abc.go")
        if err != nil {
            log.Fatalln("Error")
        }
        fmt.Println(file)
        defer file.Close()
        data := make([]byte, 100)
        count, err := file.Read(data)
        if err != nil {
            log.Fatalln("Error")
        }
        fmt.Println(count, string(data))
    }
}

将文件加载并在终端中显示

func thirdPartyConnectDB(){
    panic("unable to connect DB!!")
}

func save(){
    defer func(){
        s := recover()
        fmt.Println(s)
    }()
    thirdPartyConnectDB()
}

func main(){
    save()
    fmt.Println("OK?")

}
‘‘‘
广告
将在 10 秒后关闭
bannerAds