Factory method code

package factory import "fmt" type Company interface { Goodjob() } type Thinry struct { } func (m *Thinry) Goodjob() { FMT.Println("Shine at 9.30 ")} type Shine struct {} func (m *Shine) Goodjob() {FMT.Println("Shine at 9.30 ")} func NewConpany(cType string) Company { switch cType { case "jit": return &Thinry{} case "shine": return &Shine{} } return nil }Copy the code

Test code

package factory

import "testing"

func TestNewCompany(t *testing.T) {
	NewConpany("jit").Goodjob()
	NewConpany("shine").Goodjob()
}
Copy the code

Test Execution result

Thinry will start work at 9:00 and Shine will start work at 9:30Copy the code