2019-10-10,Golang程序入口

aside section ._1OhGeD · · 1541 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

通过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的垃圾回收机制。


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:aside section ._1OhGeD

查看原文:2019-10-10,Golang程序入口

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1541 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传