美文网首页
2019-10-10,Golang程序入口

2019-10-10,Golang程序入口

作者: onmeiei | 来源:发表于2019-10-10 11:18 被阅读0次

    通过Beego的阅读,学习了基本的golang的语法及开发思路。
    再通过Go SDK的源码,熟悉一下API构成,然后开始实现一个简单的低代码框架。

    Go SDK中的package列表

    还是挺多的,挑几个学习一下,以此为教材学习一下Golang。

    还是以debug,从程序开始学习。

    runtime/proc.go

    runtime/proc.go作为Go程序的执行入口,看一下里面都有什么。

        if GOARCH != "wasm" { // no threads on wasm yet, so no sysmon
            systemstack(func() {
                newm(sysmon, nil)
            })
        }
    

    可以看到,Go是支持WebAssembly的,可以在浏览器中调用。
    具体做法请参考:
    https://github.com/golang/go/wiki/WebAssembly

    golang执行的主要几个步骤如下:

    //执行runtime_init()
    runtime_init()
    //启动GC
    gcenable()
    //执行main_init()
    fn := main_init
    fn()
    //执行main函数,此时程序进入用户的代码
    fn = main_main
    fn()
    

    接下来,看一下gcenable()都做了什么事情。

    // gcenable is called after the bulk of the runtime initialization,
    // just before we're about to start letting user code run.
    // It kicks off the background sweeper goroutine and enables GC.
    func gcenable() {
        c := make(chan int, 1)
        go bgsweep(c)
        <-c
        memstats.enablegc = true // now that runtime is initialized, GC is okay
    }
    

    gcenable启动了Golang的垃圾回收器sweeping

    runtime/mgcsweep

    下一篇文章主要看一下Golang的垃圾回收机制。

    相关文章

      网友评论

          本文标题:2019-10-10,Golang程序入口

          本文链接:https://www.haomeiwen.com/subject/umsopctx.html