Advanced Language Features Basics
10 examples to get you started with Advanced Language Features - 7 basic and 3 intermediate.
Search across all documentation pages
10 examples to get you started with Advanced Language Features - 7 basic and 3 intermediate.
go mod init example.com/advlang.assets/ folder next to packages that use go:embed.go build / go run .; set GOOS / GOARCH to exercise platform files.Bake a config file into the binary at compile time.
package main
import (
_ "embed"
"fmt"
)
//go:embed assets/version.txt
var version string
func main() {
fmt.Println("version:", version)
}//go:embed must sit immediately above a string, []byte, or embed.FS variable.Related: //go:embed for Files and Templates - FS and template patterns
Serve multiple files from an in-memory filesystem.
package main
import (
"embed"
"fmt"
"io/fs"
)
//go:embed assets/*
var content embed.FS
func main() {
data, err := fs.ReadFile(content, "assets/hello.txt")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}embed.FS paths use forward slashes and include the embedded directory prefix.fs.Sub to strip a prefix for http.FileServer or template.ParseFS... paths are rejected at compile time.Compile a file only on Linux.
//go:build linux
package main
import "fmt"
func platform() string { return "linux" }
func main() {
fmt.Println(platform())
}//go:build over legacy // +build comments in new code.&&, ||, and ! (for example linux && amd64).Related: Build Constraints & Platform-Specific Files - suffix conventions
Let the toolchain pick main_windows.go vs main_unix.go automatically.
// main_unix.go
//go:build unix
package main
func greeting() string { return "unix" }// main_windows.go
//go:build windows
package main
func greeting() string { return "windows" }// main.go
package main
import "fmt"
func main() {
fmt.Println(greeting())
}_linux.go and _amd64.go imply constraints without an explicit line.go list -f '{{.GoFiles}}' . to see which files a build selected.Gate expensive tests behind -tags integration.
//go:build integration
package myapp_test
import "testing"
func TestLiveAPI(t *testing.T) {
t.Log("runs only with -tags integration")
}go test -tags=integration ./....Inspect what go tool supports on your installation.
go help buildmodego build -buildmode=exe -o bin/app .
file bin/appexe (default), c-archive, c-shared, plugin, shared.plugin and some modes are OS/arch limited.go build line in CI logs.Related: Linker Flags & buildmode Options - mode-by-mode guide
Shrink release binaries by omitting symbol tables.
go build -ldflags="-s -w" -o bin/app .
ls -lh bin/app-s omits the symbol table; -w omits DWARF debug info.Load templates from embed.FS without disk IO at runtime.
package main
import (
"embed"
"html/template"
"os"
)
//go:embed templates/*.html
var tplFS embed.FS
func main() {
tpl, err := template.ParseFS(tplFS, "templates/*.html")
if err != nil {
panic(err)
}
_ = tpl.Execute(os.Stdout, map[string]string{"Name": "Go"})
}ParseFS glob paths are relative to the FS root you embedded.Call assembly from Go in the same package (illustrative add).
// add.go
package main
import "fmt"
func add(x, y int64) int64
func main() {
fmt.Println(add(3, 4))
}// add_amd64.s
TEXT ·add(SB), NOSPLIT, $0-24
MOVQ x+0(FP), AX
ADDQ y+8(FP), AX
MOVQ AX, ret+16(FP)
RET· names the Go symbol..s files; most code should stay in Go.Related: Assembly in Go (*.s files) - calling conventions
Prevent inlining when measuring call overhead.
package bench
//go:noinline
func Work() int {
return 42
}//go:noinline is for tests, benchmarks, and rare ABI boundaries.go test -bench and profiles before micro-tuning.Related: Compiler Directives: //go:noinline, linkname & go:fix inline - full directive set
Stack versions: This page was written for Go 1.26.x (Green Tea GC default, go fix modernizers - verify patch at build), chi (latest - verify at build), gin (latest - verify at build), echo (latest - verify at build), google.golang.org/grpc (latest - verify at build), sigs.k8s.io/controller-runtime (latest - verify at build), kubebuilder (latest - verify at build), tinygo (latest - verify board targets at build), wazero (latest - verify at build), and golangci-lint (latest - verify linter set at build).
Reviewed by Chris St. John·Last updated Jul 18, 2026