重点解析一下main.go代码

//go:generate go run -tags generate gen.gopackagemainimport("fmt""log""net""net/http""os""os/signal""sync""github.com/zserge/lorca")// Go types that are bound to the UI must be thread-safe, because each binding// is executed in its own goroutine. In this simple case we may use atomic// operations, but for more complex cases one should use proper synchronization.typecounterstruct{sync.Mutexcountint}func(c*counter)Add(nint){c.Lock()deferc.Unlock()c.count=c.count+n}func(c*counter)Value()int{c.Lock()deferc.Unlock()returnc.count}funcmain(){ui,err:=lorca.New("","",480,320)iferr!=nil{log.Fatal(err)}deferui.Close()// A simple way to know when UI is ready (uses body.onload event in JS)ui.Bind("start",func(){log.Println("UI is ready")})// Create and bind Go object to the UIc:=&counter{}ui.Bind("counterAdd",c.Add)ui.Bind("counterValue",c.Value)// Load HTML.// You may also use `data:text/html,<base64>` approach to load initial HTML,// e.g: ui.Load("data:text/html," + url.PathEscape(html))ln,err:=net.Listen("tcp","127.0.0.1:0")iferr!=nil{log.Fatal(err)}deferln.Close()gohttp.Serve(ln,http.FileServer(FS))ui.Load(fmt.Sprintf("http://%s",ln.Addr()))// You may use console.log to debug your JS code, it will be printed via// log.Println(). Also exceptions are printed in a similar manner.ui.Eval(`
console.log("Hello, world!");
console.log('Multiple values:', [1, false, {"x":5}]);
`)// Wait until the interrupt signal arrives or browser window is closedsigc:=make(chanos.Signal)signal.Notify(sigc,os.Interrupt)select{case<-sigc:case<-ui.Done():}log.Println("exiting...")}

3.2详解function.go文件

packagemainimport("github.com/libragen/ginbro/parser""sync")// Go types that are bound to the UI must be thread-safe, because each binding// is executed in its own goroutine. In this simple case we may use atomic// operations, but for more complex cases one should use proper synchronization.typeguiFunctionstruct{sync.Mutexresultstring}typeargsstruct{MysqlUserstring`json:"mysqlUser"`MysqlPasswordstring`json:"mysqlPassword"`MysqlAddrstring`json:"mysqlAddr"`MysqlDatabasestring`json:"mysqlDatabase"`MysqlCharsetstring`json:"mysqlCharset"`OutPackagestring`json:"outPackage"`AppListenstring`json:"appListen"`AuthTablestring`json:"authTable"`AuthPasswordstring`json:"authPassword"`}packagemainimport("github.com/libragen/ginbro/parser""sync")// Go types that are bound to the UI must be thread-safe, because each binding// is executed in its own goroutine. In this simple case we may use atomic// operations, but for more complex cases one should use proper synchronization.typeguiFunctionstruct{sync.Mutexresultstring}typeargsstruct{MysqlUserstring`json:"mysqlUser"`MysqlPasswordstring`json:"mysqlPassword"`MysqlAddrstring`json:"mysqlAddr"`MysqlDatabasestring`json:"mysqlDatabase"`MysqlCharsetstring`json:"mysqlCharset"`OutPackagestring`json:"outPackage"`AppListenstring`json:"appListen"`AuthTablestring`json:"authTable"`AuthPasswordstring`json:"authPassword"`}func(c*guiFunction)MysqlGen(argargs)string{c.Lock()deferc.Unlock()ng,err:=parser.NewGuiParseEngine(arg.MysqlUser,arg.MysqlPassword,arg.MysqlAddr,arg.MysqlDatabase,arg.MysqlCharset,arg.OutPackage,arg.AppListen,arg.AuthTable,arg.AuthPassword)iferr!=nil{returnerr.Error()}iferr:=ng.ParseDatabaseSchema();err!=nil{returnerr.Error()}ng.GenerateProjectCode()ng.GoFmt()return"your ginbro project is created at "+ng.OutPath}{c.Lock()deferc.Unlock()ng,err:=parser.NewGuiParseEngine(arg.MysqlUser,arg.MysqlPassword,arg.MysqlAddr,arg.MysqlDatabase,arg.MysqlCharset,arg.OutPackage,arg.AppListen,arg.AuthTable,arg.AuthPassword)iferr!=nil{returnerr.Error()}iferr:=ng.ParseDatabaseSchema();err!=nil{returnerr.Error()}ng.GenerateProjectCode()ng.GoFmt()return"your ginbro project is created at "+ng.OutPath}

type args struct 用来序列号vuejs发送来的参数

type guiFunction struct 必须继承 sync.Mutex ,解决线程不安全问题

3.3详解main.go文件

//go:generate go run -tags generate gen.gopackagemainimport("fmt""github.com/zserge/lorca""log""net""net/http""os""os/signal")funcmain(){ui,err:=lorca.New("","",480,600)iferr!=nil{log.Fatal(err)}deferui.Close()// A simple way to know when UI is ready (uses body.onload event in JS)ui.Bind("start",func(){log.Println("UI is ready")})// Create and bind Go object to the UIc:=&guiFunction{}ui.Bind("mysqlGen",c.MysqlGen)// Load HTML.// You may also use `data:text/html,<base64>` approach to load initial HTML,// e.g: ui.Load("data:text/html," + url.PathEscape(html))ln,err:=net.Listen("tcp","127.0.0.1:0")iferr!=nil{log.Fatal(err)}deferln.Close()gohttp.Serve(ln,http.FileServer(FS))ui.Load(fmt.Sprintf("http://%s",ln.Addr()))// You may use console.log to debug your JS code, it will be printed via// log.Println(). Also exceptions are printed in a similar manner.ui.Eval(`
`)// Wait until the interrupt signal arrives or browser window is closedsigc:=make(chanos.Signal)signal.Notify(sigc,os.Interrupt)select{case<-sigc:case<-ui.Done():}log.Println("exiting...")}