Hi,
I wrote a piece of code but got stuck in appending resulting strings in a string slice. How do I complete this code?
Thanks
Trying to achieve this:
ServiceName
Addresses
Logger/Level
HTTP/Server/Host
HTTP/Server/Port
HTTP/Server/Request/Timeout
HTTP/Server/Request/Size
Incomplete code:
package main
import (
"fmt"
"reflect"
)
type Config struct {
ServiceName string
Addresses []string
Logger struct {
Level string
}
HTTP struct {
Server struct {
Host string
Port int
Request struct {
Timeout string
Size int
}
}
}
}
func main() {
walk(Config{}, 0)
}
func walk(input any, level int) {
source := reflect.ValueOf(input)
for i := 0; i < source.NumField(); i++ {
field := source.Field(i)
if !field.CanInterface() {
continue
}
fmt.Println(source.Type().Field(i).Name)
if field.Kind() == reflect.Struct {
walk(field.Interface(), level+4)
}
}
}