Go语言的特点

Python034

Go语言的特点,第1张

类型 在变量名后边

也可不显式声明类型, 类型推断, 但是是静态语言, name一开始放字符串就不能再赋值数字

方法,属性 分开 方法名首字母大写就是就是外部可调的

面向对象设计的一个重要原则:“优先使用组合而不是继承”

Dog 也是Animal , 要复用Animal 的属性和方法,

只需要在结构体 type 里面写 Animal

入口也是main, 用用试试

多态, 有这个方法就是这个接口的实现, 具体的类 不需要知道自己实现了什么接口,

使用: 在一个函数调用之前加上关键字go 就启动了一个goroutine

创建一个goroutine,它会被加入到一个全局的运行队列当中,

调度器 会把他们分配给某个 逻辑处理器 的队列,

一个逻辑处理器 绑定到一个 操作系统线程 ,在上面运行goroutine,

如果goroutine需要读写文件, 阻塞 ,就脱离逻辑处理器 直接 goroutine - 系统线程 绑定

编译成同名.exe 来执行, 不通过虚拟机, 直接是机器码, 和C 一样, 所以非常快

但是也有自动垃圾回收,每个exe文件当中已经包含了一个类似于虚拟机的runtime,进行goroutine的调度

默认是静态链接的,那个exe会把运行时所需要的所有东西都加进去,这样就可以把exe复制到任何地方去运行了, 因此 生成的 .exe 文件非常大

1、解压压缩包到go工作目录,如解压到E:\opensource\go\go,解压后的目录结构如下:

E:\opensource\go\go

├─api

├─bin

│ ├─go.exe

│ ├─godoc.exe

│ └─gofmt.exe

├─doc

├─include

├─lib

├─misc

├─pkg

├─src

└─test

2、增加环境变量GOROOT,取值为上面的go工作目录

3、Path环境变量中添加"%GOROOT%\bin",以便能够直接调用go命令来编译go代码,至此go编译环境就配置好了

注:如果不想手动设置系统环境变量,也可下载go启动环境批处理附件,

修改goenv.bat文件中的GOROOT值为上面的go工作目录后直接双击该bat文件,go编译环境变量即设置完成。

4、测试go编译环境,启动一个cmd窗口,直接输入go,看到下面的提示就是搭建成功了

E:\opensource\go\go>go

Go is a tool for managing Go source code.

Usage:

go command [arguments]

The commands are:

build compile packages and dependencies

clean remove object files

doc run godoc on package sources

env print Go environment information

fix run go tool fix on packages

fmt run gofmt on package sources

get download and install packages and dependencies

install compile and install packages and dependencies

listlist packages

run compile and run Go program

testtest packages

toolrun specified go tool

version print Go version

vet run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

gopath GOPATH environment variable

packagesdescription of package lists

remote remote import path syntax

testflagdescription of testing flags

testfuncdescription of testing functions

Use "go help [topic]" for more information about that topic.

5、编译helloworld测试程序,go语言包中test目录带有helloworld.go测试程序,源码见"附一 helloworld.go",

直接调用"go build helloworld.go"就生成了"helloworld.exe"可执行程序,运行一下这个程序看到了我们期望的hello,wolrd。

E:\opensource\go\go\test>go build helloworld.go

E:\opensource\go\go\test>helloworld.exe

hello, world

E:\opensource\go\go\test>

附一 helloworld.go

// cmpout

// Copyright 2009 The Go Authors. All rights reserved.

// Use of this source code is governed by a BSD-style

// license that can be found in the LICENSE file.

// Test that we can do page 1 of the C book.

package main

func main() {

print("hello, world\n")

}