Go Quick Guide

Source: Internet
Author: User

Golang, as a static language, has advantages in web-side development and high concurrency, and is a modern language developed by Google.

Code structure and its simple operation

How to install the Golang, the Internet to find a lot of their own. General Go Project code is how to organize, how to perform tests, how to compile projects, how to execute, and how to reference outsourcing.

Create a project directory

Create a working directory Calcproject

<calcproject>工作目录         <src>源代码目录(其他项目目录放在此文件夹中)                <calc> 项目目录                         main.go 主函数文件                 <simplemath> 工具目录                         add.go                          add_test.go 测试文件                         sqrt.go                        sqrt_test.go        <bin>二进制目录        <pkg>引用包的编译

Code

Main.go

   package main  import (      "os"      "fmt"      "simplemath"      "strconv"  )  var Usage = func(){      fmt.Println("USAGE: cal command [argument]..")  }func main(){      args := os.Args      if args == nil || len(args) < 2{          Usage()          return      }      switch args[1]{          case "add":              fmt.Println("[0]:%s",args[1])              v1, err1 :=strconv.Atoi(args[2])              v2, err2 := strconv.Atoi(args[3])              if err1 != nil || err2 != nil{                  fmt.Println("USAGE: cal add int1 int2")                  return              }              ret := simplemath.Add(v1, v2)              fmt.Println("Result:", ret)          default:              Usage()      }  }

In fact, the Add method is not necessarily in the Add file, can also be implemented in sqrt, it is visible that the go is in the Simplemath package search public methods.

Sqrt.go

package simplemathfunc  Add(a int, b int) int {    return a+b}

Simplemath the other files in the file, be sure to specify the package Simplemath otherwise it will be an error

Set the execution environment for GO

The go execution environment relies on the path of the Gopath, otherwise you will get an error when executing the Go Build Calc project, and you cannot find the project. So add GOPATHD =/xx/xxx/calproject;source ~/.BASHRC in ~/.BASHRC . Equivalent to specifying a working directory, go Build calc

Test

Go Test Simplemath will locate the test file and do the testing. GDB Calc can also be debugged in one step

Get third-party packages

Go get github.com/xxx, but you will find the speed super slow how to do

go get -u github.com/gpmgo/gopm//下载完成之后用go build xxx,生成gopm文件,放在系统bin目录中gopm get  golang.org/x/net  下载

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.