Learning Go language

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

Starter: personal blog, update & error correction & reply

Start to learn a little go language, the language is said to be in the country than in foreign fire, the community of mixed, but the "Pony across the river" Well, always have to first try to draw a conclusion.

Environment Preparation:

1. Download and install the Go language in Golang China, personal like to install with MSI, directly help you set the environment variables, lest manually.
2. Install Goclipse: Download the latest version of Eclipse on the Eclipse website, enter the menu help, install new software after installation, type the URL http://goclipse.github.io/ Releases/. Be careful to select Goclipse in the long list of readings, and do not choose all (blood lessons), then go all the way next.
3. After installing the Goclipse, select the Go menu in the Eclipse configuration (Window, Preferences), and the goroot and Gopath themselves, the former is the root of the go, The latter specifies a directory on its own.
4. If you cannot run the git command on the console, the GIT client is not installed on your PC, you will need to download msysgit and install it to prepare for the 5th step.
5. Click on the menu go-tools, click Download Gocode large button, will automatically download the installation Gocode, and put in the previous step you specified Gopath.
6. Download GDB (I want to save the trouble directly under CSDN), put the exe file into the system path, that is, let the command line run GDB to take effect (I put it directly into the bin under the Go folder), so that goclipse can debug breakpoints.

Learning experience:
Here is the go language of the Chinese document, the beginning to read seven cattle out of the "Go language programming" personally feel good, the book appendix has "Common Package Introduction", to follow the document to see very good.

Syntactically, the general evaluation is "lackluster", not fancy, take the pragmatic route, reading the 1th chapter mentioned, automatic garbage collection, rich built-in type, function multiple return value, error handling, anonymous function closure, type interface, concurrent programming, reflection , are relatively basic, Some of the most popular syntactic sugars in dynamic languages, in addition to multiple return values, do not have much of a place, but go is strongly typed.

This means that the compiler can help you do a lot of things, error checking, syntax hints, which can alleviate a lot of the burden of programmers, compiled results such as EXE program, directly to the server on the bare metal can run, unlike Python ah Nodejs they need to now target machine on the Ann language, Reduced workload (also published a web program, in addition to the program, Java needs a Java environment and an application server, Nodejs need a Nodejs language environment, and go do not need to Ann), but if you run four compiled programs on bare metal, rely on the library is four parts of the volume , and only one copy of Python, everyone is common. But that's not very important.

It is important that go the best to do the network server's ability, using the lightweight process, capable of a lot of concurrency (millions of), and the association is the language of natural support, start a new process directly go Xxfun, Xxfun is a function, go function is a class object.
Network programming with the basic library, the syntax is very simple and heinous, the 5th chapter of reading, the individual HelloWorld as follows (more advanced has not studied):

Package Main

Import (
"Bufio"
"FMT"
"Io/ioutil"
"Log"
"NET"
"Net/http"
"OS"
)

Func Main () {
Httpserver ()
HttpClient ()
TcpClient ()
TCPServer ()
Test ()
}

Func Test () {
CH: = Make (chan int, 1)
for{
Select {
Case CH <-0:
Case CH <-1:
}
I: = <-ch
Fmt. Println ("Value", i)
}
}

Func cmd () {
for {
Reader: = Bufio. Newreader (OS. Stdin)
Input, _: = Reader. Readbytes (' \ n ')
println (String (Input[0:len (input)-1))
}
}

//http server
func Httpserver () {
    http. Handlefunc ("/hello", func (w http). Responsewriter, R *http. Request) {
        W.write ([]byte ("" Hello ")]
   })
}

//http client
func HttpClient () {
    Client: = &http. CLIENT{}
    Reqest, _: = http. Newrequest ("GET", "http://427studio.net", nil)
    Response, _: = client. Do (reqest)
    if response. StatusCode = =
        Body, _: = Ioutil. ReadAll (response. Body)
        BODYSTR: = string (body)
        FMT. Println (BODYSTR)
   }
}

TCP Client
Func tcpClient () {
Conn, _: = Net. Dial ("TCP", "182.92.10.238:42769")//write
Fmt. fprintf (conn, "getgames\n")

Data, _: = Bufio. Newreader (conn). ReadString (' \ n ')//Read
Fmt. Printf ("% #v \ n", data)
}

TCP Server
Func TCPServer () {
ln, ERR: = Net. Listen ("TCP", ": 6010")
If err! = Nil {
Panic (ERR)
}
for {
Conn, err: = ln. Accept ()
If err! = Nil {
Log. Fatal ("Get Client Connection Error:", err)
}
Go _service (conn)
}
}
Service functions used by TCP servers
Func _service (Conn net. Conn) {
Data, err: = Bufio. Newreader (conn). ReadString (' \ n ')
If err! = Nil {
Log. Fatal ("Get Client Data error:", err)
}
Fmt. Printf ("% #v \ n", data)
Fmt. fprintf (conn, "Hello client\n")
Conn. Close ()
}

Func Checkerr (e error) {
If E! = Nil {
Fmt. fprintf (OS. Stderr, "%s", E.error ())
}
}

Currently learning craved, tomorrow to think of an idea to do server + multi-client command line game to play.

Long-term welcome project Cooperation Opportunity Introduction, project income 10% to reward introducer. Sina Weibo: @ Cold Mirror, qq:908789432.
Related Article

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.