今天是一次做Go的笔记,一开始直接打开Github上的Go项目然后跑到Wiki位置,然后作者列出了一堆学习Go的资料,这里我

以第一个学习资料https://tour.golang.org/作为Go学习到入门。然后为了训练我的终端运用

能力还有Vim下的编码能力这里我使用到了tmuxVim编辑器,然后之前已经在VIM里面安装了Vim-go插件了,所以在之前

的Go项目文件目录下可以直接使用命令模式使用:Go 来执行相应的操作。如果项目报错都话你可能是没有按照

Vim-go的要求现在项目里面执行: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上,涉及到墙的问题。自动安装可能会失败。当然你有梯子的话除外;我们可以手工安装,进入到GOPATH的SRC目录下,运行命令 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所需要的文件了

最后出现如图所示:

项目一 Packages

package main

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

然后在Vim下面执行:Go Run即可输出

My favorite number is 1

项目二 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.

项目三 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

项目四 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

项目五 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

项目六 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

项目七 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

项目八 Variables

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

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

项目九 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!

项目十 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 (varfunc, 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!

项目十一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 intuint, 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)

项目十二 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 ""

项目十三 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

项目十四 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 intfloat64, 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

项目十五 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

项目十六 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


    <Br>
<Br>
<Br> 发现更多更好玩的,欢迎关注我的微信公众号:<span style='color:red;'> FullStacker </span><br /> <Br>
<Br>
<Br>
<img src="https://raw.githubusercontent.com/itgoyo/PicGoRes/master/img/%E6%A0%BC%E9%B2%81%E7%89%B9toolpool.jpg"
height="400px" width="400px" />
</div>

Vim编辑器Go简单入门的更多相关文章

  1. Windows & Linux 安装使用 Vim 编辑器 3分钟入门 - 精简归纳

    Windows & Linux 安装使用 Vim 编辑器 3分钟入门 - 精简归纳 JERRY_Z. ~ 2020 / 8 / 25 转载请注明出处! 目录 Windows & Lin ...

  2. Linux系统Vi/Vim编辑器的简单介绍、安装/卸载、常用命令

    Linux系统Vi/Vim编辑器的简单介绍.安装/卸载.常用命令 1.介绍 vi(Visual Interface)编辑器是Linux和Unix上最基本的文本编辑器,工作在字符模式下.由于不需要图形界 ...

  3. git介绍以及一些常用命令,加上vim编辑器的简单使用

    https://www.jianshu.com/p/04a6517869b4 vim:进入vim编辑器,如果后接文件名,则进入该文件的编辑模式,看图:①.vim编辑器中,按i进入编辑模式:②.按Esc ...

  4. unity 编辑器扩展简单入门

    unity 编辑器扩展简单入门 通过使用编辑器扩展,我们可以对一些机械的操作实现自动化,而不用使用额外的环境,将工具与开发环境融为一体:并且,编辑器扩展也提供GUI库,来实现可视化操作:编辑器扩展甚至 ...

  5. Linux Vim编辑器使用简单讲解

    在Linux中,主要编辑器为vi或者vim,本文围绕vim做简单的讲解说明:Linux默认自带vi(vim)编辑器,其程序包为:[root@linuxidc.com ~]# rpm -qf `whic ...

  6. vim编辑器的简单使用

    写这篇文章是因为在更新我的一篇博客 Git的其他用法 的时候,里面的修改已经提交的commit说明这一部分需要用到vim. 在使用git config --global --edit或者git reb ...

  7. vim编辑器-Linux从入门到精通第四天(非原创)

    文章大纲 一.vi介绍二.vim三种模式(重点)三.命令模式四.模式间的切换(重点)五.末行模式六.编辑模式七.实用功能八.扩展九.学习资料下载十.参考文章   一.vi介绍 Vi编辑器是所有Unix ...

  8. (大数据工程师学习路径)第二步 Vim编辑器----高级功能入门

    一.多文件编辑 1.使用vim编辑多个文件 编辑多个文件有两种形式,一种是在进入vim前使用的参数就是多个文件.另一种就是进入vim后再编辑其他的文件. 同时创建两个新文件并编辑 $ vim 1.tx ...

  9. vim编辑器最简单使用方法

    i 输入模式 :q 不保存退出 :q! 强制退出 :wq 保存退出 j 下 k 上 h 左 l 右 gg start G end x 往后删 X 往前删 yy 复制行 p 粘贴 dd 剪切行 u 撤销 ...

随机推荐

  1. D - Denouncing Mafia DFS

    这道题其实很简单,求k个到根的链,使得链上的节点的个数尽可能多,如果节点被计算过了,就不能再被计算了,其实我们发现,只要k>=叶子节点,那么肯定是全部,所以我们考虑所有的叶子节点,DFS到根节点 ...

  2. 洛谷P2504 [HAOI2006]聪明的猴子

    #include<bits/stdc++.h> using namespace std; ; ; int n,m,k,ans; double Max; int monkey[maxn]; ...

  3. Redis源码解析:03字典

    字典是一种用于保存键值对(key value pair)的抽象数据结构.在字典中,一个键和一个值进行关联,就是所谓的键值对.字典中的每个键都是独一无二的,可以根据键查找.更新值,或者删除整个键值对等等 ...

  4. 开发者说:如何参与定义一款 IDE 插件

    摘要: If not now,when? If not you,who?共同定义 Cloud Toolkit 的未来! 自从产品经理银时小伙和他的开发小哥们在去年12月发布 Cloud Toolkit ...

  5. 解决大数据难题 阿里云MaxCompute获科技大奖

    摘要: 据介绍,MaxCompute(大规模分布式的数据计算平台)是国内最早自研的大数据计算平台之一,主要应用于大规模数据处理场景.目前,这项源自浙江.解决世界级难题的成果已拥有EB(百京)级别的数据 ...

  6. 从HelloWorld看Knative Serving代码实现

    摘要: Knative Serving以Kubernetes和Istio为基础,支持无服务器应用程序和函数的部署并提供服务.我们从部署一个HelloWorld示例入手来分析Knative Servin ...

  7. HZOJ 星际旅行

    正解欧拉路,其实看完题解还是挺简单的,由于对欧拉路这种东西没怎么接触过,所以考试时没想出来,知识还是有漏洞啊. 另外这题的题解写的也不是很清楚(可能大佬作者觉得这是一道送分题……),首先判断联通(注意 ...

  8. @noi - 172@ 追捕大象

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 在一块平原上有一头大象. 平原被分成 n×m 个格子.初始时大象 ...

  9. 史上最全的MYSQL备份方法

    本人曾经 用过的备份方式有:mysqldump.mysqlhotcopy.BACKUP TABLE .SELECT INTOOUTFILE,又或者备份二进制日志(binlog),还可以是直接拷贝数据文 ...

  10. CODE FESTIVAL 2017 qual A B fLIP(补题)

    平时没见过这样的题目,看到后很懵逼.没想到. 思路:按下按钮的顺序并不影响结果,一个按钮要么按一次,要么不按,按多了也没用,比如:按3次和按1次没啥区别. 假设这是个M * N的矩阵,我们已经按下了k ...