1. 程式人生 > 程式設計 >go開發中引用靜態庫.a檔案的方法

go開發中引用靜態庫.a檔案的方法

前言

我使用goland開發,下面都是用goland做演示

一、生成demo.a

新建一個專案,目錄如下

在這裡插入圖片描述

demo.go

package demo

import (
	"fmt"
)

func Demo() {
	fmt.Printf("hello world")
}

main.go

package main

import "demo"

func main() {
	demo.Demo()
}

配置Run/Debug Configurations,在Go tool arguments:後輸入-i,然後執行後就會生成demo.a

在這裡插入圖片描述
在這裡插入圖片描述

二、修改demo.go

在檔案頭新增//go:binary-only-package,新增這個之後就不會編譯了,這個在go/build/doc.go檔案中最下方有說明

demo.go

//go:binary-only-package

package demo

import (
	_ "fmt"
)

func Demo() {
}
//	//go:binary-only-package
//
//	package mypkg
//
// The source code may include additional Go code. That code is never compiled
// but will be processed by tools like godoc and might be useful as end-user
// documentation.

執行後發現可以正常呼叫到demo.a裡面的Demo函式

在這裡插入圖片描述

提示:必須匯入demo裡用到的包,要不然會報錯

到此這篇關於go開發中引用靜態庫.a檔案的方法的文章就介紹到這了,更多相關go引用靜態庫.a檔案內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!