首頁  >  文章  >  後端開發  >  golang中.a檔是什麼

golang中.a檔是什麼

尚
原創
2019-12-16 15:44:585536瀏覽

golang中.a檔是什麼

.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檔是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn