golang use static files (.a file) compiled

The general process of the program: to edit, compile, link, run. Because golang of a project source code is open source, we rarely take care to compile, link problems. If a non-open source third-party libraries in a project, this time packed out how a program exe executable?

golang compiled using a static file

使用go tool compile

golang using a static file links

Use go tool link

//staticpkg/print.go
package staticpkg

import "fmt"

func Hello() {
    fmt.Println("hello")
}

//运行 go install 会生成对应的.a文件

//修改print.go文件,去掉函数实现,
//main/main.go
package main

import (
    "staticpkg"
)

func main() {
    staticpkg.Hello()
}

//使用tool的compile命令编译当前项目
go tool compile -I $GOPATH/windows_amd64 main.go 
//会生成一个main.o文件

//使用tool的link命令生成可执行文件
go tool link -o main.exe -L $GOPATH/windows_amd64 main.go
//会生成一个main.exe程序,直接运行会输出hello

//如果直接go run main.go此时没有任何输出

When using a static file (.a file) compile and link, just to have the corresponding .a file. .A file but is unable to view some definition to the package, if you want to use .a file for development, but also a corresponding declaration file.

https://github.com/golangaccount/exporthead is a simple export items go package declaration, which removes the implementation details and type or interface function in non-export item

Guess you like

Origin www.cnblogs.com/zp900704/p/11907504.html