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

And running the program using beta version of Go 1.16.

go1.16beta1 run embed-file.go

Hello, Gophers!

Reading file content in bytes format.

//go:embed "hello.txt"
var b []byte

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")