Go Builder pattern, [go Design pattern]
Direct code:
Ackage mainimport "fmt" / / now there are classrooms and dormitories, with two classrooms, windows two meters high and a dormitory door. The window is one meter high type Room struct {doorNum int windowHeight int} func (r * Room) showRoomInfo () {fmt.Println ("door num:", r.doorNum, "door windowHeight:", r.windowHeight)} type RoomBuilder interface {build () * Room} func BuildRoom (builder RoomBuilder) * Room {return builder.build ()} type Cla***oomBuilder struct {} func (* Cla***oomBuilder) build () * Room {return & Room {2 2}} type DormitoryBuilder struct {} func (* DormitoryBuilder) build () * Room {return & Room {1,1}} func main () {room: = BuildRoom (& Cla***oomBuilder {}) room.showRoomInfo () room = BuildRoom (& DormitoryBuilder {}) room.showRoomInfo () / similarly, we need a living room, a door without windows. You can write a living room builder / / the example here is very simple, but in practice, the build function may deal with very complex things. Through the builder mode, you can separate the specific / / instance from the creation process, so it is easy to extend and replace the creation process} github long-term update: https://github.com/fanux/gopatterns