[Go] struct
package main
import "fmt"
type User struct {
ID int
FirstName string
LastName string
Email string
}
func main() {
u := User{ID: 1, FirstName: "Z", LastName: "H", Email: "zh@gmail.com"}
fmt.Println(u)
}
User
is captizaled, so it is auto exported.
package main
import "fmt"
// User is a user type
type User struct {
ID int
FirstName, LastName, Email string
}
// Group represents a set of users
type Group struct {
role string
users []User
newestUser User
spaceAvailable bool
}
func describeUser(u User) string {
desc := fmt.Sprintf("Name: %s %s, Email: %s", u.FirstName, u.LastName, u.Email)
return desc
}
func describeGroup(g Group) string {
desc := fmt.Sprintf("This user group has %d. The newser user is %s %s. Accepting new users: %t", len(g.users), g.newestUser.FirstName, g.newestUser.LastName, g.spaceAvailable)
return desc
}
func main() {
u := User{ID: 1, FirstName: "Mark", LastName: "Doel", Email: "mark.doel@gmail.com"}
u2 := User{ID: 2, FirstName: "Danel", LastName: "Petter", Email: "danel.petter@gmail.com"}
g := Group{
role: "admin",
users: []User{u, u2},
newestUser: u2,
spaceAvailable: true,
}
fmt.Println(describeUser(u)) // Name: Mark Doel, Email: mark.doel@gmail.com
fmt.Println(describeGroup(g)) // This user group has 2. The newser user is Danel Petter. Accepting new users: true
}