Embed Files in Go using "embed package" in go 1.16 version

In Go 1.16 version, using package “embed” we can easily embed files in Go programs.

The embed file content variable should be of type string, []byte, or FS only.

package main

import _"embed"


func main() {
	//go:embed "hello.txt"
	var s string
	print(s)
}

And running the program using beta version of Go 1.16.

go1.16beta1 run embed-file.go

//OUTPUT
Hello, Gophers!

Reading file content in bytes format.

//go:embed "hello.txt"
var b []byte
print(string(b))

To read the file contents in embed.FS format use the below code snippet.

//go:embed hello.txt
var f embed.FS
data, _ := f.ReadFile("hello.txt")
print(string(data))