Go language development, step by Step (Part 1: From Hello World to web development)

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

Through the previous article "Yesterday Once more---again encounter go language", you can build the Go language development environment.

Now start the Go Language (Golang) program development, here we Go.

Example 1. The simplest program let's Go:

Package main//There must was a so-called main package for a Go exe.import  "FMT"//FMT was a package including printfu NC Main () {//the entry of a Go EXE FMT. Print ("Hello,let s Go")//main Program Context}


The program code is very simple, anyone who has the skills of programming can understand.

Run in Eclipse to see the print results in the console:

Hello,let ' s Go


Example 2. Parallel program (multi-process, can be easily understood as multithreaded programs)

(* Note 1, the co-worker, lightweight thread also, because of its lightweight, the system in the process can be millions). Specifically implemented here is not a fine table.

* Note 2, the purple color code indicates that this example is changed from the above example, while the purple itself code represents the modified/added code. This is more convenient for readers to read. )

This is basically a prototype of the concurrent program in the go language, but as you may have seen, the master process exits after creating the coprocessor, so it may not be possible for the coprocessor to execute at all.

Package main//There must was a so-called main package for a Go exe.import  "FMT"//FMT was a package including printfu NC coroutine () {//coroutine functionfmt. Println ("Go")}func Main () {//the entry of a go Exe//main program contextfmt. Println ("Hello,let ' s ..."); Go Coroutine ();}



Example 3. Introducing a synchronization mechanism to implement real parallel programs

In the Golang, the channel can be used to realize the inter-process communication, the mutex in the sync packet can be used to realize the synchronization between the Rwmutex and the resource sharing, and the channel is able to synchronize between the processes.

The channel in Golang is similar to that of a pipeline in Linux.

Package main//There must is a so-called main package for a Go exe.import  


(The *go program does not have to use a semicolon for the end of each sentence; but I used to add a semicolon.) )
This program creates 10 threads, communicates between the main program and the coprocessor, and synchronizes with each other.

Output:

Hello,let ' s ... go 0go 1go 2go 3go 4go 5go 6go 7go 8go 9Main process gets from Coroutine:0main process gets from coroutine : 1Main process gets from Coroutine:2main process gets from Coroutine:3main process gets from Coroutine:4main process g ETS from Coroutine:5main process gets from Coroutine:6main process gets from Coroutine:7main process gets from Coroutin E:8main Process gets from Coroutine:9


Example 4. Add Advanced tips: Use anonymous functions + multicore support

(* Note that the anonymous function is not associated with multicore at all, just to use this 2 technique in one example)


In this way, we implement a complete parallel program that runs on multicore systems.
The output of this example on my system (Intel Core 2 Duo) is:

Hello,let ' s ... go 0go 4go 5go 6go 7Main process gets from Coroutine:0go 2go 8go 9go 1Main process gets from coroutine:1m Ain Process gets from Coroutine:2go 3Main process gets from Coroutine:3main process gets from Coroutine:4main process g ETS from Coroutine:5main process gets from Coroutine:6main process gets from Coroutine:7main process gets from Coroutin E:8main Process gets from Coroutine:9


Example 5. Make one of the simplest sites, let ' s go!

Webserver listen to port 8080, when you receive the client's Yes, respond.


The program can be executed in eclipse, or it can be executed on the command line, such as.

The client accesses the http://localhost:8080 with IE browser and writes the message "yes" to port 8080.

(* Note, I encountered a problem is my anti-virus software has been blocked my main program, in this case, need to temporarily turn off the protection)

The first part ends here, and the intention is to use vivid examples to arouse curiosity or affection for the language, including myself.

Reference documents

1. golang.org

2. "Go language Programming" Xu Shiwei Lu Guihua, People's post and telecommunications press

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.