.a文件是编译过程中生成的,每个package都会生成对应的.a文件,go在编译的时候先判断package的源码是否有改动,如果没有的话,就不再重新编译.a文件,这样可以加快速度。
生成.a文件(.h文件也会自动生成)
创建文件pkgqrcode.go
package mainimport "c"import ( //"fmt" "github.com/tuotoo/qrcode" "os")//export getqrcodestringfunc getqrcodestring(cstring *c.char) *c.char { //func getqrcodestring() *c.char { path := c.gostring(cstring) //path := "qrcode.png" fi, err := os.open(path) if err != nil { //fmt.println(err.error()) return c.cstring(path) } defer fi.close() qrmatrix, err := qrcode.decode(fi) if err != nil { //fmt.println(err.error()) return c.cstring(path) } //fmt.println(qrmatrix.content) //return c.cstring(qrmatrix.content) gostr := qrmatrix.content cstr := c.cstring(gostr) return cstr}func main() {}
import "c" 的作用就是go代码中使用c函数
需要加//export getqrcodestring 才会生成.h文件(不知道什么!!)
c.gostring(cstring) 把c字符串转成go字符串
c.cstring(gostr) 把go字符串转成c字符串
编译步骤
生成.a文件命令(进入pkgqrcode.go代码目录)执行:
go build -buildmode=c-archive -o pkgqrcode.a pkgqrcode.go
生成结果
pkgqrcode.a
pkgqrcode.h
更多golang知识请关注golang教程栏目。
以上就是golang中.a文件是什么的详细内容。