I am really new to Go, so want some advice. I have a struct:
type Employee struct {
Name string
Designation string
Department string
Salary int
Email string
}
I want to concatenate the string fields into a type of employee description. So that, I can say: toString(employee) and get:
John Smith Manager Sales john.smith@example.com
I tried to fetch each field, check if they are empty and put them in a slice and join them at the end
employeeDescArr := make([]string, 0, 4)
if strings.TrimSpace(value) != "" {
append(employee.GetName(), value)
}...
return strings.Join(employeeDescArr[:], " ")
I think this method is very verbose and shows lack of Go skills. Is it better to use a string Builder instead? Is there a way to iterate through all fields of a struct in a Reflection way and join them?