This is a creation in Article, where the information may have evolved or changed.
When you first see the GO program build executable exe files on the Windows platform, it declares that the Windows app must also be the battlefield of the Go language. Go is not a scripting language, but it has a lightweight and simple feature of the scripting language. The go language is truly a "dream of application development for dynamic languages" compared to scripting languages such as PHP and Python that use server consoles as the main battlefield.
Windows Api
Windows desktop apps rely on the win API to draw a variety of application interfaces and controls that essentially invoke Windows-provided APIs. The first thing that go does to develop Windows apps is to encapsulate these Windows APIs.
Https://github.com/lxn/go-winapi
This project has already achieved the encapsulation of the WINAPI. For example, you will find the CreateWindowEx package in Go-winapi/user32.go:
Here is the use of the Syscall package. To illustrate, the official documents of Golang are not syscall. SYSCALL12 description, need to look at the code, here Syscall12 represents the CreateWindowEx passed in 12 parameters, the Syscall method has been implemented and
Syscall, Syscall6, Syscall9, Syscall12, Syscall15.
Specific code reference ($goroot/src/pkg/syscall/dll_windows.go, here http://codereview.appspot.com/1578041/#ps2001 You can see the SYSCALL12 code addition process and related discussions)
Control
Next, after having the basic WINAPI, you need to use the interface for each control. The official does not provide a standard library, but there are many open source projects that have already completed this package, and here are a few open source projects:
Gform:https://github.com/allendang/gform
Go-iup:https://github.com/jcowgar/go-iup
go.uik:https://github.com/skelterjohn/go.uik/
Walk:https://github.com/lxn/walk
The Lxn Walk project (Windows application Library Kit) is recommended and used here, and the walk encapsulated controls should be the most complete of these, and are constantly being perfected.
such as Bitmap, RadioButton, checkbox, Pushbutton and so on. A few examples can be found in walk/example for reference.
Realize
Well, with Go-winapi and walk two open source projects, you can start a Windows app.
The interface is as follows:
This is a simple socket IM, open two ports on a machine, 8000 and 8001, two ports listen to each other and send messages.
(Before implementing a C # version, see here http://www.cnblogs.com/yjf512/archive/2012/06/17/2552816.html)
Go version of Socket IM Source:
Https://github.com/jianfengye/MyWorks/tree/master/go_socketim
Implementation is always simple, say a few code snippets:
1 Creating the window:
1 Walk. Initialize (walk. Initparams{paniconerror: true})
2Defer walk. Shutdown ()
3
4Mainwnd, err: = walk. Newmainwindow ()
5 ifErr! = Nil {
6 return
7}
8
9MW: = &mainwindow{mainwindow:mainwnd}
Ten
OneMw. SetSize (walk. size{ -, Max})
AMw. Show ()
-Mw. Run ()
2 Creating the control:
Button1, _: = Walk. Newpushbutton (MW)
Button1. SetText ( " Start Port 8000 ")
Button1. SetX ( Ten)
Button1. Sety ( Ten)
Button1. SetWidth ( -)
Button1. SetHeight ( -)
Button1. Clicked (). Attach (func () {
Go Newtalkwindow (MW, 8000, 8001)
Button1. SetEnabled ( false)
})
Creating the UI is basically a two-step process, and of course walk has a more sophisticated way of using controls, which is not used here.
3 Business logic
Func ( This*talkwindow) Send () error {
TXT: = This. Sendtext.text ()
Conn, Err: = Net. Dial ( " TCP ", " localhost: "+ StrConv. Itoa ( This. SendPort))
ifErr! = Nil {
returnErr
}
Lenth: = Len ([] byte(TXT))
Pre: = Int32tostream (Int32 (Lenth), Bigendian)
Fmt. FPRINTF (Conn, string(pre) + txt)
This. Sendtext.settext ( "")
returnNil
}
Func ( This*talkwindow) Listen () error {
ln, ERR: = Net. Listen ( " TCP ", " : "+ StrConv. Itoa ( This. Listenport))
ifErr! = Nil {
returnErr
}
for{
Conn, err: = ln. Accept ()
ifErr! = Nil {
Continue
}
Go func () {
Buffer: = make ([] byte, 4)
Conn. Read (buffer)
Lenth: = StreamToInt32 (buffer, Bigendian)
Contentbuf: = make ([] byte, Lenth)
Conn. Read (CONTENTBUF)
Text: = Strings. Trimspace ( string(CONTENTBUF))
Fmt. Println (text)
This. Showtext.settext ( This. Showtext.text () + time. Now (). Format ( " 2006-01-02 10:13:40 ") + Breakchars + StrConv. Itoa ( This. SendPort) + " : "+ text + " \ r \ n ")
}()
}
returnNil
}
After the creation of the UI is the specific business logic, where the business logic is relatively simple, mainly using the net package to establish and listen to the TCP port.
Summarize
The use of Go compared to C # to benefit more in the logic implementation, such as in C # to open a multi-process, a process to listen to messages a process to receive messages, such an implementation is cumbersome and cumbersome, need to use the thread library. However, in Go is implemented using Goroutine, directly open a goroutine to listen to messages, the main process to send messages, it is in line with the thinking logic of programming.
Go compared to C # is not enough to say that the IDE, GO has not been able to visualize the application of the IDE. But the walk library is used skillfully, and I think this should not be a problem, and there are reasons to believe that similar Ides will appear in the near future.
Is it possible for go to support the development of mobile terminal applications in the future? Android,ios? It is said that the requirement to use go to develop Android apps has been put on the agenda, after all Google kids. As for iOS, there may be a long way to go.
PS: The updated version to 2012/11/6,walk has been walk. initialize removed, replaced by other functions, so the examples in this article should be modified accordingly
Specifically, you can see this comment.
Https://github.com/lxn/walk/commit/731093ca2543db32cba2327bce91e71aa49b6a11