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

Arunkumar Gudelli

I am One among a million Software engineers of India. I write beautiful markup.I make the Web useful.

Follow us @ twitter, facebook and linkedin For latest news and articles about Go Language

☝ ✍ Go Language Tutorial ✍☝
Get a short & sweet Go Language tutorials delivered to your inbox every couple of days. No spam ever. Unsubscribe any time.