Pkl is a config managment language developed by Apple. It is a simple language to manage configuration files. It has plugin support for popular editors and support for golang.
Sample project
Let’s start with a simple go project.
go mod init pkl-sample
touch main.go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, World!")
}
Install pkl
First, let’s install pkl using brew.
brew install pkl
Then, Let’s install pkl-go library in our project.
go get github.com/apple/pkl-go
Let’s also install codegen tool for pkl.
go install github.com/apple/pkl-go/cmd/pkl-gen-go@v0.6.0
Create a pkl config
Let’s create a pkl file pkl/AppConfig.pkl
@go.Package { name = "pkl-sample/appconfig" }
module pkl-sample.AppConfig
import "package://pkg.pkl-lang.org/pkl-go/pkl.golang@0.6.0#/go.pkl"
greeting: String = "Hello, "
message: String = "World!"
note the target package name pkl-sample/appconfig
in the @go.Package
annotation.
it will be used to generate the go code.
Generate Go code
pkl-gen-go pkl/AppConfig.pkl --base-path pkl-sample
This will generate go files appconfig/AppConfig.go
and appconfig/init.pkl.go
Use the generated code
package main
import (
"fmt"
"pkl-sample/appconfig"
)
func main() {
cfg, err := appconfig.LoadFromPath(context.Background(), "pkl/AppConfig.pkl")
if err != nil {
panic(err)
}
fmt.Println(cfg.Greeting + cfg.Message)
}
Conclusion
Although it’s a bit workaround to setup pkl, the flexibility it offers is good and it’s a good standard to follow for configuration management in any project.
It can also convert config to other formats like json, yaml, etc.
on top of it, we can use the same .pkl
file to generate code for other languages like swift, kotlin, etc.