Go language download, install, configure, use

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

Go language

The Go language (golang,the go programming Language) is the second open source programming language released by Google 2009. In July 2009, Google released the simple language, which was used to develop Android apps.

The go language is a new programming language launched by Google that can reduce the complexity of your code without compromising application performance.

Rob Pike, Google's Chief software engineer, said: "We've developed go because software development has been frustrating over the last more than 10 years."

The go language is optimized for programming multi-processor system applications, with go-compiled programs that are comparable to C or C + + code, and are more secure and support parallel processes.

Go Language official website : https://golang.org (requires VPN access)

1. Download Go

Go language is a cross-platform, support windows, Linux, Mac OS x and other systems, also provide source code, can be compiled and installed

Windows:go1.8.3.windows-amd64.msi (78MB)

Linux:go1.8.3.linux-amd64.tar.gz (86MB)

Mac:go1.8.3.darwin-amd64.tar.gz (85MB) ( recommended )

Source:go1.8.3.src.tar.gz (15MB)

2. Install Go

sudo tar-c/usr/local/-zxvf go1.8.3.darwin-amd64.tar.gz

Note: must be installed in the/usr/local/directory because goroot must be/usr/local/go

Installation directory Description:

$ ll/usr/local/go/total 232-rw-r--r--1 Homer staff 33243, 04:14 authors-rw-r--r--1 Homer staff 1366 Ma  Y 04:14 contributing.md-rw-r--r--1 Homer staff 45710, 04:14 contributors-rw-r--r--1 homer staff 1479 04:14 license-rw-r--r--1 Homer staff 1303 may 04:14 patents-rw-r--r--1 Homer staff 1399 May 25 04 : readme.md-rw-r--r--1 Homer staff 7 could 04:14 versiondrwxr-xr-x Homer staff 476 may 04:14 Apid Rwxr-xr-x 5 Homer Staff-04:16 Bindrwxr-xr-x 4 Homer staff 136 may 04:16 Blogdrwxr-xr-x-ho     Mer staff 1496-04:14 doc-rw-r--r--1 Homer staff 5686 3 04:14 favicon.icodrwxr-xr-x 102 04:14 Libdrwxr-xr-x Homer staff 544 could 04:16 miscdrwxr-xr-x 7 Homer staff 238 25 04 : pkg-rw-r--r--1 Homer staff, 04:14 Robots.txtdrwxr-xr-x, Homer staff 2312, may 04:14 srcdrwx R-xr-x 269 Homer Staff   9146 Jul 01:29 Test 

Directory file Explanation:

API   -Directory, contains all API lists, convenient for the IDE to use Bin   -directory, store compiled executable blog  -directory, Doc   -directory, help document Lib   -directory, Misc  -Directory, pkg   -Directory, which holds the compiled package file. The files in the pkg are the SRC-directories generated by Go compilation, the   project source file test  -Directory, the sample code note: Generally, the bin and pkg directories can not be created, the GO command will be created automatically (such as Go install), only need to create the SRC directory. AUTHORS         -File, author list, open contributing.md-file with Notepad, CONTRIBUTORS    -file, LICENSE         -file, LICENSE, open with Notepad patents         -File, readme.md       -file,         version-file, release info, open favicon.ico with Notepad     -file, robots.txt      -file, Use robots.txt to block access to URLs, details view Https://support.google.com/webmasters/answer/6062608?hl=zh-Hans

Eclipse installs the Go plugin and searches the market for "go", such as installing Goclipse

Note: Goclipse Plugin Development Program, need to restart the computer before it will take effect , because the/etc/profile configuration environment variable Restart the computer before it takes effect

3. Configure Go

Vim/etc/profile

To add a configuration:

Goroot=/usr/local/go
Export path= $GOROOT/bin: $PATH

To make the configuration effective:

Source/etc/profile

Verify that the installation was successful:

$ go version
Go version go1.8.3 darwin/amd64

4. Use Go

Basic syntax

Go language = and: What is the difference

= is an assignment,: = is the declaration variable and assigns a value.

= Use must use the first VAR declaration for example: Var aa=100//or var B = 100//or var c int = 100//: = is declared and assigned, and the system automatically infers the type, does not require the var keyword d: = 100

1) Hello Example

Vim Hello.go

Package main import "FMT" Func Main () {   fmt. Printf ("Hello mimvp.com")}

Compile execution:

Go Build
./hello

Operation Result:

$ ./hello
Hello mimvp.com

2) Crawling page example

Vim Spider_web.go

/*** mimvp.com* 2017.4.12** spider web of Mimvp.com*/package mainimport ("FMT" "Io/ioutil" "Net/http") func main () { Response, _: = http. Get ("http://mimvp.com") defer response. Body.close () body, _: = Ioutil. ReadAll (response. Body) fmt. Println (String (body)) fmt. Printf ("Len:%d\n", Len (String));}

Compile execution:

$ go Build spider_web.go
$./spider_web

<! DOCTYPE html>
<meta charset= "Utf-8" >

...

</body>

len:15661

5. Proxy Example

The go language supported proxies are HTTP and socks5, and HTTPS and SOCKS4 are not supported

Where go uses the SOCKS5 agent, you need to install the reference "Golang.org/x/net/proxy", the installation configuration steps are as follows:

1. Download https://github.com/golang/net

2. Unzip and move to/usr/local/go/src/golang.org/x/net/

3. Import ("Golang.org/x/net/proxy")

The Go language Agent example is as follows (all tested, successful):

/*** mimvp.com* 2017.7.20* Golang use HTTP, socks5 proxy */package mainimport ("FMT" "Io/ioutil" "Log" "Net/http" "Net/url" " Os "" Golang.org/x/net/proxy ")//Success http//Proxy_uri: =" http://125.77.25.124:80 "//Mimvp_url: =" http://mimvp.com " Func main_1 (Proxy_uri string, Mimvp_url string) {proxy: = Func (_ *http. Request) (*url. URL, error) {return URL. Parse (Proxy_uri)}transport: = &http. Transport{proxy:proxy}client: = &http. CLIENT{TRANSPORT:TRANSPORT}RESP, Err: = client. Get (Mimvp_url) if err! = Nil {fmt. Println ("Error:", err) return} else {defer resp. Body.close () body, _: = Ioutil. ReadAll (resp. Body) fmt. Printf ("%s\n", body)}}//success http//Proxy_uri: = "http://125.77.25.124:80"//Mimvp_url: = "Http://mimvp.com" func Main_2 (Proxy_uri string, Mimvp_url string) {url_i: = URL. Url{}url_proxy, _: = Url_i.parse (Proxy_uri) Transport: = &http. Transport{proxy:http. Proxyurl (url_proxy)}client: = http. CLIENT{TRANSPORT:TRANSPORT}RESP, Err: = client. Get (Mimvp_url) if err! = Nil {log. Fatalln (ERR)} else {defer resp. Body.close () body, _: = Ioutil. ReadAll (resp. Body) fmt. Printf ("%s\n", body)}}//config Environment Varablefunc main_22 (Proxy_uri string, Mimvp_url string) {//url_i: = URL. url{}//Url_proxy, _: = Url_i.parse ("https://127.0.0.1:9743") os. Setenv ("Http_proxy", "http://125.77.25.124:80") os. Setenv ("Https_proxy", "https://210.209.89.100:8081") c: = http. client{/* Transport: &http. transport{//Proxy:http. Proxyurl (Url_proxy)} */}resp, err: = C.get ("http://mimvp.com") if err! = Nil {log. Fatalln (Err)} else {defer resp. Body.close () body, _: = Ioutil. ReadAll (resp. Body) fmt. Printf ("%s\n", body)}}//Specifies the proxy ipfunc gettransportfieldurl (proxy_addr *string) (transport *http). Transport) {url_i: = URL. Url{}url_proxy, _: = Url_i.parse (*proxy_addr) transport = &http. Transport{proxy:http. Proxyurl (url_proxy)}return}//get HTTP proxy address from environment variable $http_proxy or $http_proxy//Linux Set Agent environment variable command://http_proxy=http:// 125.77.25.124:80//Https_proxy=https://210.209.89.100:8081func gettransportfromenvironment () (Transport *http. Transport) {Transport = &http. Transport{proxy:http. Proxyfromenvironment}return}func fetch (Mimvp_url, Proxy_uri *string) (HTML string) {transport: = Gettransportfieldurl ( Proxy_uri) Client: = &http. Client{transport:transport}req, Err: = http. Newrequest ("GET", *mimvp_url, nil) if err! = Nil {log. Fatal (Err. Error ())}resp, err: = client. Do (req) if err! = Nil {log. Fatal (Err. Error ())}if resp. StatusCode = = Robots, err: = Ioutil. ReadAll (resp. Body) resp. Body.close () if err! = Nil {log. Fatal (Err. Error ())}html = string (robots)} else {html = ""}return}//success http//Proxy_uri: = "http://125.77.25.124:80"//Mimvp_ur L: = "http://mimvp.com" func main_3 (Proxy_uri string, Mimvp_url string) {html: = Fetch (&mimvp_url, &proxy_uri) FMT . PRINTLN (HTML)}//success socks5//1. Download https://github.com/golang/net//2. Unzip and move to/usr/local/go/src/golang.org/x/net///3. Import ("Golang.org/x/net/proxy") Func main_socks5 (Proxy_socks string, Mimvp_url string) {//Create A socks5 Dialerdialer, err: = Proxy. SOCKS5 ("TCP", Proxy_socks, Nil, proxy.) Direct) If err! = Nil {fmt. Println (OS. Stderr, "can ' t connect to SOCKS5 Proxy:", err) OS. Exit (1)}//setup a http client//transport: = &http. Transport{}//transport. Dial = Dialer. Dialtransport: = &http. Transport{dial:dialer. Dial}client: = &http. CLIENT{TRANSPORT:TRANSPORT}RESP, Err: = client. Get (Mimvp_url) if err! = Nil {log. Fatalln (Err)} else {defer resp. Body.close () body, _: = Ioutil. ReadAll (resp. Body) fmt. Printf ("%s\n", body)}}func main () {//http Proxyproxy_uri: = "http://125.77.25.124:80" Mimvp_url: = "http://mimvp.com" Main_1 (Proxy_uri, Mimvp_url) main_2 (Proxy_uri, Mimvp_url) main_3 (Proxy_uri, Mimvp_url)//SOCKS5 Proxyproxy_socks: = " 175.138.65.244:1080 "Main_socks5 (Proxy_socks, Mimvp_url)}

The agents in this example, all from the M-topology agent

Meter flapping agent, provide professional high-quality agent, support HTTP, HTTPS, SOCKS4, SOCKS5 and other protocols,

Covering more than 120 countries, China 34 provinces and cities, agent quality and availability is very high

m-Flapping agent : http://proxy.mimvp.com

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.