亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
Using the embed package (Go 1.16 )
Step 1: Import the embed package
Step 2: Use //go:embed to include files
Example: Embed a single file
Example: Embed multiple files into a slice of strings
Example: Embed a directory into embed.FS
Handling binary files
Using embed.FS for structured file access
Rules and tips
Build-time considerations
Alternative: Using external tools (pre-Go 1.16 or advanced use)
首頁 後端開發(fā) Golang 如何在golang二進制中嵌入文件

如何在golang二進制中嵌入文件

Aug 04, 2025 pm 04:12 PM
golang 文件嵌入

Go 1.16及以上版本推薦使用embed包將文件嵌入二進製文件中。 1. 導(dǎo)入embed包;2. 在變量聲明前使用//go:embed指令指定要嵌入的文件或目錄;3. 根據(jù)文件類型選擇string(文本文件)、[]byte(二進製文件)或embed.FS(多個文件或目錄)作為變量類型;4. 路徑為相對於Go源文件的目錄;5. 使用標(biāo)準(zhǔn)I/O函數(shù)從embed.FS讀取文件內(nèi)容;6. 編譯時無需特殊標(biāo)誌,嵌入文件會自動包含在可執(zhí)行文件中,且運行時不可修改。

How to embed files in a Golang binary

Embedding files into a Go binary allows you to distribute a single, self-contained executable that includes assets like HTML templates, configuration files, images, or JavaScript. Starting with Go 1.16, the embed package makes this straightforward.

How to embed files in a Golang binary

Using the embed package (Go 1.16 )

The recommended way to embed files is using the //go:embed directive along with the embed package.

Step 1: Import the embed package

 import (
    "embed"
    "net/http"
)

Step 2: Use //go:embed to include files

You can embed:

How to embed files in a Golang binary
  • A single file
  • Multiple files
  • An entire directory (including subdirectories)
Example: Embed a single file
 //go:embed version.txt
var version string

func main() {
    println("Version:", version)
}
Example: Embed multiple files into a slice of strings
 //go:embed config/*.json
var configFiles []string

func main() {
    for _, file := range configFiles {
        println(file)
    }
}

Note: This only works for text files (like JSON, YAML, etc.). Binary files (eg, images) must be embedded as []byte .

Example: Embed a directory into embed.FS
 //go:embed assets/*
var assetFS embed.FS

func main() {
    http.Handle("/static/", http.FileServer(http.FS(assetFS)))
    http.ListenAndServe(":8080", nil)
}

In this example, all files in the assets/ directory are embedded and served via an HTTP file server.

How to embed files in a Golang binary

The path after //go:embed is relative to the package directory.

Handling binary files

For binary files (images, binaries, etc.), use []byte :

 //go:embed logo.png
var logo []byte

func main() {
    _ = os.WriteFile("copy.png", logo, 0644)
}

Using embed.FS for structured file access

You can read files from the embedded filesystem using standard I/O functions:

 //go:embed templates/*
var templateFS embed.FS

func renderTemplate(name string) (string, error) {
    content, err := templateFS.ReadFile("templates/" name)
    if err != nil {
        return "", err
    }
    return string(content), nil
}

Rules and tips

  • The //go:embed directive must be on the line immediately before a variable declaration .
  • Only works with package-level variables .
  • Supported types:
    • string → single text file
    • []byte → single binary file
    • embed.FS → one or more files/directories
  • Paths are relative to the Go source file's directory .
  • Embedded files are included in the binary at compile time — they cannot be modified at runtime.

Build-time considerations

No special build flags are needed. Just use normal go build :

 go build -o myapp .

The embedded files are automatically included.

Alternative: Using external tools (pre-Go 1.16 or advanced use)

Before Go 1.16, people used tools like:

  • go-bindata
  • statik
  • packr

But these are now largely obsolete thanks to //go:embed .

If you need features like compression or encryption, you can still use such tools, but for most use cases, embed is sufficient and standard.


Basically, with //go:embed , embedding files is simple and idiomatic in modern Go. Just declare a variable, add the directive, and use the embed.FS or raw types as needed.

以上是如何在golang二進制中嵌入文件的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
Golang vs.C:性能和速度比較 Golang vs.C:性能和速度比較 Apr 21, 2025 am 12:13 AM

Golang適合快速開發(fā)和並發(fā)場景,C 適用於需要極致性能和低級控制的場景。 1)Golang通過垃圾回收和並發(fā)機制提升性能,適合高並發(fā)Web服務(wù)開發(fā)。 2)C 通過手動內(nèi)存管理和編譯器優(yōu)化達到極致性能,適用於嵌入式系統(tǒng)開發(fā)。

Golang和C:並發(fā)與原始速度 Golang和C:並發(fā)與原始速度 Apr 21, 2025 am 12:16 AM

Golang在並發(fā)性上優(yōu)於C ,而C 在原始速度上優(yōu)於Golang。 1)Golang通過goroutine和channel實現(xiàn)高效並發(fā),適合處理大量並發(fā)任務(wù)。 2)C 通過編譯器優(yōu)化和標(biāo)準(zhǔn)庫,提供接近硬件的高性能,適合需要極致優(yōu)化的應(yīng)用。

Go語言中哪些庫是由大公司開發(fā)或知名的開源項目提供的? Go語言中哪些庫是由大公司開發(fā)或知名的開源項目提供的? Apr 02, 2025 pm 04:12 PM

Go語言中哪些庫是大公司開發(fā)或知名開源項目?在使用Go語言進行編程時,開發(fā)者常常會遇到一些常見的需求,?...

Golang vs. Python:性能和可伸縮性 Golang vs. Python:性能和可伸縮性 Apr 19, 2025 am 12:18 AM

Golang在性能和可擴展性方面優(yōu)於Python。 1)Golang的編譯型特性和高效並發(fā)模型使其在高並發(fā)場景下表現(xiàn)出色。 2)Python作為解釋型語言,執(zhí)行速度較慢,但通過工具如Cython可優(yōu)化性能。

Golang的目的:建立高效且可擴展的系統(tǒng) Golang的目的:建立高效且可擴展的系統(tǒng) Apr 09, 2025 pm 05:17 PM

Go語言在構(gòu)建高效且可擴展的系統(tǒng)中表現(xiàn)出色,其優(yōu)勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發(fā)編程:通過goroutines和channels簡化多任務(wù)處理;3.簡潔性:語法簡潔,降低學(xué)習(xí)和維護成本;4.跨平臺:支持跨平臺編譯,方便部署。

Golang vs. Python:主要差異和相似之處 Golang vs. Python:主要差異和相似之處 Apr 17, 2025 am 12:15 AM

Golang和Python各有優(yōu)勢:Golang適合高性能和并發(fā)編程,Python適用于數(shù)據(jù)科學(xué)和Web開發(fā)。Golang以其并發(fā)模型和高效性能著稱,Python則以簡潔語法和豐富庫生態(tài)系統(tǒng)著稱。

C和Golang:表演至關(guān)重要時 C和Golang:表演至關(guān)重要時 Apr 13, 2025 am 12:11 AM

C 更適合需要直接控制硬件資源和高性能優(yōu)化的場景,而Golang更適合需要快速開發(fā)和高並發(fā)處理的場景。 1.C 的優(yōu)勢在於其接近硬件的特性和高度的優(yōu)化能力,適合遊戲開發(fā)等高性能需求。 2.Golang的優(yōu)勢在於其簡潔的語法和天然的並發(fā)支持,適合高並發(fā)服務(wù)開發(fā)。

Golang vs. Python:並發(fā)和多線程 Golang vs. Python:並發(fā)和多線程 Apr 17, 2025 am 12:20 AM

Golang更適合高並發(fā)任務(wù),而Python在靈活性上更有優(yōu)勢。 1.Golang通過goroutine和channel高效處理並發(fā)。 2.Python依賴threading和asyncio,受GIL影響,但提供多種並發(fā)方式。選擇應(yīng)基於具體需求。

See all articles