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)

Go-yu-yan-xia-zai-an-zhuang-pei-zhi-shi-yong-01

    1. 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 
api   — 目录,包含所有API列表,方便IDE使用bin   — 目录,存放编译后的可执行文件blog  — 目录,doc   — 目录,帮助文档lib   — 目录,misc  — 目录,pkg   — 目录,存放编译后的包文件。pkg中的文件是Go编译生成的src   — 目录,存放项目源文件test  - 目录,示例代码

Note: In general, 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         — 文件,作者列表,用记事本打开CONTRIBUTING.md — 文件,CONTRIBUTORS    — 文件,LICENSE         — 文件,license,用记事本打开PATENTS         — 文件,README.md       — 文件,VERSION         — 文件,版本信息,用记事本打开 favicon.ico     — 文件,robots.txt      — 文件,使用robots.txt阻止对网址的访问,

View More https://support.google.com/we ...

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

go-yu-yan-xia-zai-an-zhuang-pei-zhi-shi-yong-02

Note: Goclipse plugin development Program, need to restart the computer before it takes effect, because the/etc/profile configuration environment variable Restart the computer before it takes effect

    1. 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

    1. Using Go

Basic syntax

Go language = and: What is the difference

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

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 main import (    "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(body)));}

Compile execution:

$ go Build spider_web.go
$./spider_web

<!DOCTYPE html>
    1. 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 main import ("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)}}//Specify 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 Settings 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: = Gettransportfiel Durl (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_url: = "http://mimvp.com" func main_3 (Proxy_uri string, Mimvp_url string) {html: = f Etch (&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 dialer Dialer, 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. Dial Transport: = &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 proxy Proxy_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 Proxy proxy_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.