# How to traverse all Table of Tables on the go-toml

# Example toml

Assuming you have a toml config file like this:

[dbhandle]
  [dbhandle.dbs]
    [dbhandle.dbs.mariadb]
      dataSourceName = "id:pw@/db"
    [dbhandle.dbs.sqlite]
      file = "ml"

How can I travers all dbs with go-toml

# Anser

You can get keys of Table with Keys (opens new window) function. With this, you can travers all dbs as follows:


 









	dbs := dbhandleConfig.Get("dbhandle.dbs").(*toml.Tree)
	for _, dbName := range dbs.Keys() {
		db := dbs.Get(dbName).(*toml.Tree)
		for _, key := range db.Keys() {
			log.Println("dbName:", dbName)
			log.Println("key:", key)
			log.Println("value:", db.Get(key))
		}

	}

The result is as followns:

2021/04/20 11:25:14 main.go:99: dbName: mariadb
2021/04/20 11:25:14 main.go:100: key: dataSourceName
2021/04/20 11:25:14 main.go:101: value: id:pw@/db
2021/04/20 11:25:14 main.go:99: dbName: sqlite
2021/04/20 11:25:14 main.go:100: key: file
2021/04/20 11:25:14 main.go:101: value: ml

# references


Last Updated: 4/20/2021, 11:26:32 AM