This tutorial explains reading and writing to toml file in Golang
go-toml is a Go language parser for reading and writing toml files.
First, Install the go-toml library.
Open the terminal, install the module
$ go get github.com/pelletier/go-toml
go: downloading github.com/pelletier/go-toml v1.9.5
go: added github.com/pelletier/go-toml v1.9.5
Let’s c
How to read toml files content in Golang
We can read TOML files in Golang in multiple ways.
Let’s have a config.toml file
[mysql]
host = "localhost:31016"
username = "root"
password = "password"
database = "students"
First way, read individual values from toml file
- import go-toml from the github repository
- toml.LoadFile() loads the toml file and reads into a variable, It throws an error if any IO exception
- You can access the individual values using Get with the given key. Nested values can be get using key.nestedkey.
Here is an example
package main
import (
"fmt"
"github.com/pelletier/go-toml"
)
func main() {
// Reading from a TOML file
data, err := toml.LoadFile("config.toml")
if err != nil {
fmt.Println("Error reading TOML file:", err)
return
}
// Accessing values from the TOML tree
value := data.Get("mysql")
fmt.Println("data : ", value)
// Access individual values
host := data.Get("mysql.host")
fmt.Println("host : ", host)
password := data.Get("mysql.password")
fmt.Println("password : ", password)
username := data.Get("mysql.username")
fmt.Println("username : ", username)
}
Output:
data : database = "students"
host = "localhost:31016"
password = "password"
username = "root"
host : localhost:31016
password : password
username : root
Second Way, Parse toml content into Struct.
- Define struct in golang similar to the structure of some content
- First, read the toml file using the
os.ReadFile()
function, it throws an error if the IO exception - It reads into a variable
- Pass the variable and struct reference to
toml.Unmarshal()
function. - toml content is converted to Struct type and access the keys using
dot
notation.
Here is an example
package main
import (
"fmt"
"os"
"github.com/pelletier/go-toml"
)
type Database struct {
host string
username string
password string
database string
}
func main() {
// Reading from a TOML file
var db Database
// Read the toml file content
doc, err := os.ReadFile("config.toml")
if err != nil {
fmt.Println("Error reading TOML file:", err)
return
}
err1 := toml.Unmarshal(doc, &db)
if err1 != nil {
fmt.Println("Error convert TOML to Struct:", err1)
return
}
// Accessing values from the TOML tree
fmt.Println("Host:", db.host)
fmt.Println("Username:", db.username)
fmt.Println("Password:", db.password)
fmt.Println("Database:", db.database)
}
How to write to toml file in Golang
Multiple ways we can write to the TOML file, you can use inline data creation with toml.tree() function, another using struct type and create a file.
- Toml provides Tree function to take data
- Create data using the
Tree()
constructor - call the
WriteFile()
function on the tree data - It creates a toml file with the content.
package main
import (
"fmt"
"github.com/pelletier/go-toml"
)
func main() {
// Creating a TOML tree
data := toml.Tree{
"mysql": toml.Tree{
"host": "localhost",
"password": "root",
"username": "root",
"database": "students"
},
}
// Writing the TOML tree to a file
err := data.WriteFile("result.toml")
if err != nil {
fmt.Println("Error create TOML file:", err)
}
}
you can also use Struct to write a toml file