I have a bulk of string that has two columns split by space, first columns is username and second column is password. I want to format that string to a slice of User
struct
The string is like this:
Bob qqweq
Tom erwwe
Andersen sadfadfs
The struct is simply like this:
type User struct{
Username string
Password string
}
How to do that typically with go?
Here's one way to do it:
var users []User
for _, l := range strings.Split(s, "
") {
f := strings.Fields(l)
if len(f) == 2 {
users = append(users, User{f[0], f[1]})
}
}