又捣鼓了一下Golang的GUI……还是一如既往的……
这次用了walk库(以前想用来着但是没鼓捣明白0.0)
package main import ( "github.com/lxn/walk" . "github.com/lxn/walk/declarative" "github.com/widuu/gojson" "io/ioutil" "net/http" "net/url" "strings" ) func Getmeaning(name string) string { if name == "" { return "" } address := make(url.Values) address.Set("key", name) res, _ := http.Get("http://api.uihoo.com/chengyu/chengyu.http.php?" + address.Encode() + "&format=json") defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) data := gojson.Json(string(body)) return data.Getindex(1).Get("diangu").Tostring() } func main() { var intext, outtext *walk.TextEdit MainWindow{ Title: "成语查询Golang", MinSize: Size{800, 600}, Layout: VBox{}, Children: []Widget{ HSplitter{ Children: []Widget{ TextEdit{AssignTo: &intext}, TextEdit{AssignTo: &outtext, ReadOnly: true}, }, }, PushButton{ Text: "查询", OnClicked: func() { if intext.Text() != "" { outtext.SetText("") sz := strings.Split(intext.Text(), " ") for i := 0; i <= strings.Count(intext.Text(), " "); i++ { outtext.AppendText(sz[i] + ":" + Getmeaning(sz[i]) + "\n") } } }, }, }, }.Run() }
安装:
go get github.com/lxn/walk
在你的源码目录下还需要一个*.manifest文件
例如一个叫test.manifest的文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="SomeFunkyNameHere" type="win32"/> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/> </dependentAssembly> </dependency> </assembly>
然后你需要一个叫rsrc的工具
go get github.com/akavel/rsrc
来编译manifest
rsrc -manifest test.manifest -o rsrc.syso
最后就是编译了!(废话!)
go build
如果想在运行的时候没有cmd窗口的话,那就用这个来编译。
go build -ldflags="-H windowsgui"