Home  >  Article  >  Backend Development  >  What is .a file in golang

What is .a file in golang

尚
Original
2019-12-16 15:44:585608browse

What is .a file in golang

.a file is generated during the compilation process. Each package will generate a corresponding .a file. Go When compiling, first determine whether the source code of the package is If there are changes, if not, the .a file will not be recompiled, which can speed up the process.

Generate .a file (.h file will also be automatically generated)

Create file 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() {}

The function of import "C" is When using C functions in go code

you need to add //export GetQrcodeString to generate the .h file (don’t know what!!)

C.GoString(cstring) Convert C string to go String

C.CString(gostr) Convert go string to C string

Compilation steps

Generate .a file command (enter pkgqrcode.go code directory) Execution:

go build -buildmode=c-archive -o pkgqrcode.a pkgqrcode.go

Generate results

##pkgqrcode.a

pkgqrcode.h

For more golang knowledge, please pay attention to

golang tutorialcolumn.

The above is the detailed content of What is .a file in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn