golang 简介
golang是一门强类型,高并发,易上手的编程语言。
为什么选择 golang
golang被称为云时代的c++,其极高的开发效率,天然的高并发能力,入门容易但天花板又很高。近几年来,golang在中国的火热程度极具上升,笔者所呆过的腾讯,新加坡上市外企均有大量部门转型为golang开发。
环境安装
- 下载goland
- 配置GOPATH等环境变量
- 代码路径严格按照golang推荐。
代码执行
示例代码
package main func main() int { print "hello, world\n" return 0 } 复制代码
控制台直接运行 go run hello.go
什么是package
golang 有package的概念,即目前这个程序属于哪个包。
A package is nothing but a directory with some code files, which exposes different variables (features) from a single point of reference
直观上说包就是包含了一些代码文件的目录。
为什么要有package:
Imagine you have more than a thousand functions which you need constantly while working on any project. Some of these functions have common behavior. For example, toUpperCase and toLowerCase function transforms case of a string, so you write them in a single file (probably case.go). There are other functions which do some other operations on string data type, so you write them in a separate file as well. Since you have many files which do something with string data type, so you created a directory named string and put all string related files into it. Finally, you put all of these directories in one parent directory which will be your package. The whole package structure looks like below. package main
代码结构示例
package-name ├── string | ├── case.go | ├── trim.go | └── misc.go └── number ├── arithmetics.go └── primes.go 复制代码
编译程序 go build hello.go
编译出来的二进制,可以脱离代码运行。不过不能跨平台。即windows编译出来的,无法在linux运行。
有疑问加站长微信联系(非本文作者)