embed-directive

Embed Directive

原文:https://gobyexample.com/embed-directive

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Note:
// This code is from https://gobyexample.com.
package main

import (
	"embed"
	"fmt"
)

//go:embed folder/single_file.txt
var fileString string

//go:embed folder/single_file.txt
var fileByte []byte

//go:embed folder/single_file.txt
//go:embed folder/*.hash
var folder embed.FS

func main() {
	print(fileString)       // hello go
	print(string(fileByte)) // hello go

	content1, _ := folder.ReadFile("folder/file1.hash")
	print(string(content1)) // 123

	content2, _ := folder.ReadFile("folder/file2.hash")
	print(string(content2)) // 456

	content3, _ := folder.ReadFile("folder/single_file.txt")
	fmt.Println(content3)   // [104 101 108 108 111 32 103 111 13 10]
	print(string(content3)) // hello go
}
最后修改 October 10, 2024: 更新 (a4b8f85)