使用Vim編輯器開發(fā)Go的簡單入門

今天是一次做Go的筆記从隆,一開始直接打開Github上的Go項(xiàng)目然后跑到Wiki位置,然后作者列出了一堆學(xué)習(xí)Go的資料根资,這里我
以第一個(gè)學(xué)習(xí)資料https://tour.golang.org/作為Go學(xué)習(xí)到入門盼樟。然后為了訓(xùn)練我的終端運(yùn)用
能力還有Vim下的編碼能力這里我使用到了tmuxVim編輯器,然后之前已經(jīng)在VIM里面安裝了Vim-go插件了惕艳,所以在之前
的Go項(xiàng)目文件目錄下可以直接使用命令模式使用:Go來執(zhí)行相應(yīng)的操作搞隐。如果項(xiàng)目報(bào)錯(cuò)都話你可能是沒有按照
Vim-go的要求現(xiàn)在項(xiàng)目里面執(zhí)行:GoInstallBinaries.

You will also need to install all the necessary binaries. vim-go makes it easy to install all of them by providing a command, :GoInstallBinaries, which will go get all the required binaries.

但是由于但由于 go 的代碼很多在 github 和 golang.org 上,涉及到墻的問題远搪。自動(dòng)安裝可能會失敗劣纲。當(dāng)然你有梯子的話除外;

我們可以手工安裝谁鳍,進(jìn)入到GOPATH的SRC目錄下癞季,運(yùn)行命令git clone https://github.com/golang/tools golang.org/x/tools,再接著上一步:GoInstallBinaries即可

我的解決方式是先讓終端鄒代理倘潜,至于如何讓終端走代理呢余佛,無非就是

export http_proxy=http://127.0.0.1:12333
export http_proxy=https://127.0.0.1:12333

然后在通過命令打開vim,然后再:GoInstallBinaries窍荧,這樣子就能下載Go所需要的文件了

最后出現(xiàn)如圖所示:

go.png

項(xiàng)目一

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    fmt.Println("My favorite number is", rand.Intn(10))
}

然后在Vim下面執(zhí)行:Go Run即可輸出

My favorite number is 1

項(xiàng)目二 Imports

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Printf("Now you have %g problems.\n", math.Sqrt(7))
}

輸出

Now you have 2.6457513110645907 problems.

項(xiàng)目三 Exported names

In Go, a name is exported if it begins with a capital letter. For example, Pizza is an exported name, as is Pi, which is exported from the math package.

pizza and pi do not start with a capital letter, so they are not exported.

When importing a package, you can refer only to its exported names. Any "unexported" names are not accessible from outside the package.

Run the code. Notice the error message.

To fix the error, rename math.pi to math.Pi and try it again.

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println(math.pi)
}

輸出

3.141592653589793

項(xiàng)目四 Functions

A function can take zero or more arguments.

In this example, add takes two parameters of type int.

Notice that the type comes after the variable name.

(For more about why types look the way they do, see the article on Go's declaration syntax.)

package main

import "fmt"

func add(x int, y int) int {
    return x + y
}

func main() {
    fmt.Println(add(42, 13))
}

輸出

55

項(xiàng)目五 Functions continued

When two or more consecutive named function parameters share a type, you can omit the type from all but the last.

In this example, we shortened

x int, y int

to

x, y int

package main

import "fmt"

func add(x, y int) int {
    return x + y
}

func main() {
    fmt.Println(add(42, 13))
}

輸出

55

項(xiàng)目六 Multiple results

A function can return any number of results.

The swap function returns two strings.

package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x
}

func main() {
    a, b := swap("hello", "world")
    fmt.Println(a, b)
}

輸出

world hello

項(xiàng)目七 Named return values

Go's return values may be named. If so, they are treated as variables defined at the top of the function.

These names should be used to document the meaning of the return values.

A return statement without arguments returns the named return values. This is known as a "naked" return.

Naked return statements should be used only in short functions, as with the example shown here. They can harm readability in longer functions.

package main

import "fmt"

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return
}

func main() {
    fmt.Println(split(17))
}

輸出

7 10

項(xiàng)目八 Variables

The var statement declares a list of variables; as in function argument lists, the type is last.

A var statement can be at package or function level. We see both in this example.

package main

import "fmt"

var c, python, java bool

func main() {
    var i int
    fmt.Println(i, c, python, java)
}

輸出

0 false false false

項(xiàng)目九 Variables with initializers

A var declaration can include initializers, one per variable.

If an initializer is present, the type can be omitted; the variable will take the type of the initializer.

package main

import "fmt"

var i, j int = 1, 2

func main() {
    var c, python, java = true, false, "no!"
    fmt.Println(i, j, c, python, java)
}

輸出

1 2 true false no!

項(xiàng)目十 Short variable declarations

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.

package main

import "fmt"

func main() {
    var i, j int = 1, 2
    k := 3
    c, python, java := true, false, "no!"

    fmt.Println(i, j, k, c, python, java)
}

輸出

1 2 3 true false no!

項(xiàng)目十一Basic types

Go's basic types are

bool

string

int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
// represents a Unicode code point

float32 float64

complex64 complex128

The example shows variables of several types, and also that variable declarations may be "factored" into blocks, as with import statements.

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

package main

import (
    "fmt"
    "math/cmplx"
)

var (
    ToBe   bool       = false
    MaxInt uint64     = 1<<64 - 1
    z      complex128 = cmplx.Sqrt(-5 + 12i)
)

func main() {
    fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
    fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
    fmt.Printf("Type: %T Value: %v\n", z, z)
}

輸出

Type: bool Value: false
Type: uint64 Value: 18446744073709551615
Type: complex128 Value: (2+3i)

項(xiàng)目十二 Zero values

Variables declared without an explicit initial value are given their zero value.

The zero value is:

  • 0 for numeric types,
  • false for the boolean type, and
  • "" (the empty string) for strings.
package main

import "fmt"

func main() {
    var i int
    var f float64
    var b bool
    var s string
    fmt.Printf("%v %v %v %q\n", i, f, b, s)
}

輸出

0 0 false ""

項(xiàng)目十三 Type conversions

The expression T(v) converts the value v to the type T.

Some numeric conversions:

var i int = 42
var f float64 = float64(i)
var u uint = uint(f)

Or, put more simply:

i := 42
f := float64(i)
u := uint(f)

Unlike in C, in Go assignment between items of different type requires an explicit conversion. Try removing the float64 or uint conversions in the example and see what happens.

package main

import (
    "fmt"
    "math"
)

func main() {
    var x, y int = 3, 4
    var f float64 = math.Sqrt(float64(x*x + y*y))
    var z uint = uint(f)
    fmt.Println(x, y, z)
}

輸出

3 4 5

項(xiàng)目十四 Type inference

When declaring a variable without specifying an explicit type (either by using the := syntax or var = expression syntax), the variable's type is inferred from the value on the right hand side.

When the right hand side of the declaration is typed, the new variable is of that same type:

var i int
j := i // j is an int

But when the right hand side contains an untyped numeric constant, the new variable may be an int, float64, or complex128 depending on the precision of the constant:

i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128

Try changing the initial value of v in the example code and observe how its type is affected.

package main

import "fmt"

func main() {
    v := 42 // change me!
    fmt.Printf("v is of type %T\n", v)
}

輸出

v is of type int

項(xiàng)目十五 Constants

Constants are declared like variables, but with the const keyword.

Constants can be character, string, boolean, or numeric values.

Constants cannot be declared using the := syntax.

package main

import "fmt"

const Pi = 3.14

func main() {
    const World = "世界"
    fmt.Println("Hello", World)
    fmt.Println("Happy", Pi, "Day")

    const Truth = true
    fmt.Println("Go rules?", Truth)
}

輸出

Hello 世界
Happy 3.14 Day
Go rules? true

項(xiàng)目十六 Numeric Constants

Numeric constants are high-precision values.

An untyped constant takes the type needed by its context.

Try printing needInt(Big) too.

(An int can store at maximum a 64-bit integer, and sometimes less.)

package main

import "fmt"

const (
    // Create a huge number by shifting a 1 bit left 100 places.
    // In other words, the binary number that is 1 followed by 100 zeroes.
    Big = 1 << 100
    // Shift it right again 99 places, so we end up with 1<<1, or 2.
    Small = Big >> 99
)

func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
    return x * 0.1
}

func main() {
    fmt.Println(needInt(Small))
    fmt.Println(needFloat(Small))
    fmt.Println(needFloat(Big))
}

輸出

21
0.2
1.2676506002282295e+29

Congratulations!

You finished this lesson!

You can go back to the list of modules to find what to learn next, or continue with the next lesson

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末辉巡,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蕊退,更是在濱河造成了極大的恐慌郊楣,老刑警劉巖憔恳,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異净蚤,居然都是意外死亡钥组,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進(jìn)店門今瀑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來程梦,“玉大人,你說我怎么就攤上這事橘荠∮旄剑” “怎么了?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵哥童,是天一觀的道長挺份。 經(jīng)常有香客問我,道長贮懈,這世上最難降的妖魔是什么匀泊? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮朵你,結(jié)果婚禮上各聘,老公的妹妹穿的比我還像新娘。我一直安慰自己抡医,他們只是感情好躲因,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著魂拦,像睡著了一般。 火紅的嫁衣襯著肌膚如雪搁嗓。 梳的紋絲不亂的頭發(fā)上芯勘,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天,我揣著相機(jī)與錄音腺逛,去河邊找鬼荷愕。 笑死,一個(gè)胖子當(dāng)著我的面吹牛棍矛,可吹牛的內(nèi)容都是我干的安疗。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼够委,長吁一口氣:“原來是場噩夢啊……” “哼荐类!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起茁帽,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤玉罐,失蹤者是張志新(化名)和其女友劉穎屈嗤,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吊输,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡饶号,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了季蚂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片茫船。...
    茶點(diǎn)故事閱讀 38,137評論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖扭屁,靈堂內(nèi)的尸體忽然破棺而出算谈,到底是詐尸還是另有隱情,我是刑警寧澤疯搅,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布濒生,位于F島的核電站,受9級特大地震影響幔欧,放射性物質(zhì)發(fā)生泄漏罪治。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一礁蔗、第九天 我趴在偏房一處隱蔽的房頂上張望觉义。 院中可真熱鬧,春花似錦浴井、人聲如沸晒骇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽洪囤。三九已至,卻和暖如春撕氧,著一層夾襖步出監(jiān)牢的瞬間瘤缩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工伦泥, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留剥啤,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓不脯,卻偏偏與公主長得像府怯,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子防楷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評論 2 345