Golang Introduction and Installation

Go Language


Development environment
  official website address:
    https://golang.google.cn/dl/
    choose to download and install the corresponding version
  (I use the brew so here is mac download)
    brew install Go

  use go env After completing View version


Configuring go the path environment
  mainly GOROOT and GOPATH

  GOROOT: is go installation environment
  GOPATH: search path storage destination and import the package as a compiled binary. In fact, he said the popular point is to go to your project directory. Typically GOPATH includes three directories: bin, pkg, src.

  Src directory under the main source files stored go

  pkg directory contains compiled libraries, mainly * .a file;

  The main executable file storage bin directory

  Note: Do not set the installation path to GOPATH go, you can create your own directory in the user directory, for example mygo

  After the installation is generally a good go, go env look at the use of the current environment. At this time it is showing up GOROOT that you use to install go brew installation directory, to write down this path. Next to be configured in bash_profile file.

 

Golang / Go debugging tools delve

  Windows or Linux
    go get github.com/derekparker/delve/cmd/dlv
  Mac使用brew下载
    brew install go-delve/delve/delve

     

IDE download Goland
  http://www.jetbrains.com/go/

  After downloading the activation of normal use

 


Go language project structure
  -Project
    under -src // main directory of the source file storage go
    -bin // main directory to store executable file
    -pkg // directory to store the compiled library files, mainly * .a file;

  Compile:
    cd / Project / pkg ## compiled files where compiled in there
    go build ../src/xxxx/xxx.go

 

Go code structure

package main // guide package main executable file if the replacement is not performed other name will generate a file .a calls for the introduction of other programs
 Import (
 // Go All functions are introduced by the package
 " fmt " // output module
)
main FUNC () { // main function
  fmt.Println ( " Hello Word " ) // Println to the output terminal
}
 

 

run

  Under IDE
    Right-click on Run
  a terminal
    cd to create the following file path
      go run file name.go

 

Go strong language type

  Create a function passing parameters and return values must specify types such as
    variable # supplementary declaration will be forced to use if you do not delete

func add (a int, b int // custom parameter type) int // defines {Return Type
SUM int var // Define variable type
SUM = A + B
 return SUM // Return value
}

 

Language features

 

  1. Garbage collection
    a. Automatic memory recovery, developers no longer need to manage memory
    b. Developers to focus on business hours, reducing the burden of mental
    c. Only need to allocate new memory, you do not need to release
  2. natural concurrently
    a. From the linguistic level It supports concurrent, very simple
    b.goroute, lightweight threads, created thousands of goroute possible
    c. based CSP (Commuicating Sequential Process) model

func main(){
  go fmt.Println("hello")
}

  3.channel
    A. Pipeline, similar unix / linux in the pipe
    between b. Goroute plurality of communication via Channel
    C. Any type of support

func main(){
    PIP3: = the make (int Chan, 3) // the make the length of open space 3 and can store an int
    pipe <- 1
    pipe <- 2
}

  4. Multi return value
    a. A plurality of function return value

Calc FUNC (int A, B int) (int, int) {// (int, int) define the return value
    sum:= a+ b
    avg:= (a+b)/2
    return sum,avg
}

  NOTE: unpack unwanted parameter "_" was omitted acceptable
    RES, Calc _ = (l, 3)
    RES. 4 =

  

 

 

Guess you like

Origin www.cnblogs.com/binHome/p/11862804.html