Go language Exploration first day, study notes

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

has been in the big numbers for three years of Windows C + + development, frankly speaking, I am still a small yard farming. Deeply feel oneself is a loser. Nowadays, people seldom turn on their computer to play computer after they go home, and 09 I just

When I was in college and even in 13, it was a very fast-paced internet rhythm, and the Windows terminal was already in the fall, and suddenly it became the mobile end of the world. Remember before learning to assemble, Windows PE

File, hook, thread injection and other Windows skills, the new technology of the scorn, now feel for themselves hehe. During the period I learned php,js, even the Android SDK NDK. Learn not to practice, two days forget the light. As a small yard farming, now feel a little confused, but do not know the future.

After their two days of thinking, I feel that I should learn a bit of service-side knowledge, this aspect of my friend has already reminded me many times, before I unexpectedly stubborn.

Our department's service-side development language Blossom, Lua,php,python,go, four camps, this is also due to the leadership of the inclusion and collection. Listen to people say go efficiency than c\c++ high, I heard almost no laughed. But I'm deep

Deep know, go is a very good thing.   decided to explore this stuff in my spare time. Go Boulevard to Jane, development environment set up is to decompress, configure environment variables two steps. Yes, it is said that he absorbed the advantages of various languages and rejected the shortcomings of various languages. Is that what this is about? Let's explore it a little bit ~


System Variable Name:

Goroot

Value:

Where Go is installed


System Variable Name:

Path

Append value:

;%goroot%\bin







The development environment installs the package in this Https://yunpan.cn/cRzWuKqjn9MIV access password a41e.



CMD input go print out the following a lot of information, then congratulations, the environment was built successfully. Then let's start writing our first go program ~




Package Main


Import "FMT"


Func Main () {


Fmt. Printf ("helloworld!")


}


Code Note:

Func Main () {

Can not be written

Func Main ()

{

This is not just code specification, go for unified coding style syntax.


Save file name is Gofirst.go


Compiling go build gofirst.go


You'll find one more file in the Gofirst.go directory at this time.


Drag into the cmd black box to execute.

Well? The generated file unexpectedly has 1942KB so big!!! Why is the file so big? Will go no longer follow the Windows PE fabric? The answer is in the negative.

UE looked at the next go cross compiler generated binary files, standard win PE file, it is clear that the code snippet and the resource segment added a lot of content. His flaws, this is probably what Google has to do to solve some kind of problem.




Ida looked, apparently, in order to output a Hello world go at initialization time did a lot of work.


Yes, at least in the output Hello world.  The go language should be dozens of times times slower than the C language. But it was not born to output Hello world. In addition, he is a compiled language and generates standard WinPE files for Windows. The same *nix standard execution file. We have reason to believe that in large-scale projects, put aside the memory garbage collection mechanism, (the language automatically manage garbage collection mechanism, will inevitably have a tiny impact on speed) Go has a good reason to be comparable to C, C + +, but also greatly reduce the difficulty of development, we have a lot of reasons to understand the language.



To show you the ability of the go language to rival the C language, we write a Go language Windows messbox program. The code below, um ~ The first day to write go program, this code is copy from the network Oh. In order to prove that C can do, go also can do, efficiency is not too low oh.


Package Main
Import (
"Syscall"
"Unsafe"
"FMT"
)
Func Abort (funcname string, err int) {
Panic (FuncName + "failed:" + syscall. Errno (ERR). Error ())
}
VAR (
Kernel32, _ = Syscall. LoadLibrary ("kernel32.dll")
GetModuleHandle, _ = Syscall. GetProcAddress (kernel32, "Getmodulehandlew")
User32, _ = Syscall. LoadLibrary ("User32.dll")
MessageBox, _ = Syscall. GetProcAddress (User32, "MessageBoxW")
)
Const (
MB_OK = 0x00000000
Mb_okcancel = 0x00000001
Mb_abortretryignore = 0x00000002
Mb_yesnocancel = 0x00000003
Mb_yesno = 0x00000004
Mb_retrycancel = 0x00000005
Mb_canceltrycontinue = 0x00000006
Mb_iconhand = 0x00000010
Mb_iconquestion = 0x00000020
Mb_iconexclamation = 0x00000030
Mb_iconasterisk = 0x00000040
Mb_usericon = 0x00000080
mb_iconwarning = Mb_iconexclamation
Mb_iconerror = Mb_iconhand
Mb_iconinformation = Mb_iconasterisk
Mb_iconstop = Mb_iconhand
Mb_defbutton1 = 0x00000000
Mb_defbutton2 = 0x00000100
Mb_defbutton3 = 0x00000200
Mb_defbutton4 = 0x00000300
)
Func MessageBox (caption, text string, style uintptr) (result int) {
VAR hwnd hwnd
RET, _, Callerr: = Syscall. Syscall6 (UIntPtr (MessageBox), 4,
0,//HWND
UIntPtr (unsafe. Pointer (Syscall. Stringtoutf16ptr (text))),//text
UIntPtr (unsafe. Pointer (Syscall. Stringtoutf16ptr (caption)),//Caption
Style,//type
0,
0)
If Callerr! = 0 {
Abort ("Call MessageBox", int (callerr))
}
result = Int (ret)
Return
}
Func Main () {
Defer syscall. FreeLibrary (KERNEL32)
Defer syscall. FreeLibrary (USER32)
Fmt. Printf ("Retern:%d\n", MessageBox ("Done Title", "This test is done", Mb_yesnocancel))
}
Func init () {
Fmt. Print ("Starting up\n")
}


Come on, save it as Messagebox.go.

Execute command Go build messagebox.go



The same file is not small, perhaps contains debugging information? We'll explore this later. The operation results are as follows



Note that we have called the

Kernel32, _ = Syscall. LoadLibrary ("kernel32.dll")
GetModuleHandle, _ = Syscall. GetProcAddress (kernel32, "Getmodulehandlew")
User32, _ = Syscall. LoadLibrary ("User32.dll")
MessageBox, _ = Syscall. GetProcAddress (User32, "MessageBoxW")


See, this is the same as c\c++ calling a Microsoft API. is to get the address of the MessageBox in memory, and then

Func MessageBox (caption, text string, style uintptr) (result int) {
       //var hwnd hwnd
& nbsp      ret, _, Callerr: = Syscall. Syscall6 (UIntPtr (MessageBox), 4,
               0,//HWND
    &NB Sp          uintptr (unsafe. Pointer (Syscall. Stringtoutf16ptr (text)),//text
               uintptr (unsafe. Pointer (Syscall. Stringtoutf16ptr (caption)),//caption
               style,//Type
&NBSP ;              0,
               0)
       if Callerr! = 0 {
               abort ("Call MessageBox", I NT (CALLERR))
       }
       result = Int (ret)
      &NBS P;return
}



Close the code to know that go calls the MessageBox, and C, C + + do exactly the same steps. Syscall. Syscall6 exactly do something ~ must be simply to get syscall. The first parameter of the SYSCALL6 is messbox in the Windows memory location (the location of the binary code snippet), which indicates that the called MessageBox has four parameters, that is, push four times, and then take the four parameters of the following to carry out the stick, Last Call

Push style

Push caption

Push text

Push 0

Call MessageBox




But what I'm curious about is that I simply play a messagebox, but this process starts with 6 threads




Put aside Sogou input method to inject a thread that might start without saying, at least in · In the code that go compiles,

Memory offsets messagebox.exe+0x4af80 and messagebox.exe+0x4b240 threads in this place, which are theoretically generated by the go language.

Are these threads the thread pool designed for go language high concurrency?

Let's take a closer look at what's going on in the go language gourd.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.