安装
使用版本管理器器安装:g,Go 的多版本管理工具
配置
设置代理
加快 go 相关包的下载
七牛云
1
2
|
go env -w GO111MODULE=on # 启用模块
go env -w GOPROXY=https://goproxy.cn,direct # 加快go模块下载速度
|
GOPATH(可选)
设置 go
家目录:添加 GOPATH
环境变量
Linux:
1
2
|
echo 'export GOPATH=~/codespace/goProjects' >> ~/.bashrc
source ~/.bashrc
|
在 GOPATH
目录下,通常有三个子目录:
src
: 存放源代码。
pkg
: 存放编译后的包。
bin
: 存放 go 下载的可执行文件。
1
2
|
cd ~/codespace/goProjects
mkdir -p $GOPATH/src $GOPATH/pkg $GOPATH/bin
|
为了方便使用 Go 下载的可执行文件,将 $GOPATH/bin
加入到PATH
中(如 ~/.bashrc
或 ~/.zshrc
):
1
2
3
|
# 适用于unix系统 bash环境
echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc
source ~/.bashrc
|
文件
Go 语言提供文件处理的标准库提供了:
os
:与操作系统的交互实现
io
:读写文件 io 操作
fs
:文件系统的抽象
打开
os 库提供了两个函数
- Open:返回一个文件指针和一个错误
- OpenFile:对 Open 的封转,增加了更多粒度的控制
os.Open()
定义
1
2
3
4
5
6
7
|
// Open opens the named file for reading. If successful, methods on
// the returned file can be used for reading; the associated file
// descriptor has mode O_RDONLY.
// If there is an error, it will be of type *PathError.
func Open(name string) (*File, error) {
return OpenFile(name, O_RDONLY, 0)
}
|
示例
1
2
3
4
5
6
7
8
|
func open() {
file, err := os.Open("README.md")
if err != nil {
panic(err)
}
defer file.Close()
fmt.Println(file)
}
|
os.OpenFile()
定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// OpenFile is the generalized open call; most users will use Open
// or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.). If the file does not exist, and the O_CREATE flag
// is passed, it is created with mode perm (before umask). If successful,
// methods on the returned File can be used for I/O.
// If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
testlog.Open(name)
f, err := openFileNolog(name, flag, perm)
if err != nil {
return nil, err
}
f.appendMode = flag&O_APPEND != 0
return f, nil
}
|
flag 表示描述符,可选的有
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// Flags to OpenFile wrapping those of the underlying system. Not all
// flags may be implemented on a given system.
const (
// Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
O_RDONLY int = syscall.O_RDONLY // open the file read-only.
O_WRONLY int = syscall.O_WRONLY // open the file write-only.
O_RDWR int = syscall.O_RDWR // open the file read-write.
// The remaining values may be or'ed in to control behavior.
O_APPEND int = syscall.O_APPEND // append data to the file when writing.
O_CREATE int = syscall.O_CREAT // create a new file if none exists.
O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist.
O_SYNC int = syscall.O_SYNC // open for synchronous I/O.
O_TRUNC int = syscall.O_TRUNC // truncate regular writable file when opened.
)
|
perm 表示文件的权限,可选有
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
const (
ModeDir = fs.ModeDir // d: 目录
ModeAppend = fs.ModeAppend // a: 只能添加
ModeExclusive = fs.ModeExclusive // l: 专用
ModeTemporary = fs.ModeTemporary // T: 临时文件
ModeSymlink = fs.ModeSymlink // L: 符号链接
ModeDevice = fs.ModeDevice // D: 设备文件
ModeNamedPipe = fs.ModeNamedPipe // p: 具名管道 (FIFO)
ModeSocket = fs.ModeSocket // S: Unix 域套接字
ModeSetuid = fs.ModeSetuid // u: setuid
ModeSetgid = fs.ModeSetgid // g: setgid
ModeCharDevice = fs.ModeCharDevice // c: Unix 字符设备, 前提是设置了 ModeDevice
ModeSticky = fs.ModeSticky // t: 黏滞位
ModeIrregular = fs.ModeIrregular // ?: 非常规文件
// 类型位的掩码. 对于常规文件而言,什么都不会设置.
ModeType = fs.ModeType
ModePerm = fs.ModePerm // Unix 权限位, 0o777
)
|
示例
1
2
3
4
5
6
7
8
|
func openFile() {
// 指定只读的方法打开,权限为 0644
file, err := os.OpenFile("README.md", os.O_RDONLY, 0644)
if err != nil {
panic(err)
}
fmt.Println(file)
}
|
读取
打开文件后,就可以读取文件内容了
有以下方法可以读取文件
- 文件对象.Read()
- 文件对象.ReadAt()
- os.ReadFile()
- io.ReadAll()
文件对象.Read()
定义
1
2
3
4
5
6
7
8
9
10
|
// Read reads up to len(b) bytes from the File and stores them in b.
// It returns the number of bytes read and any error encountered.
// At end of file, Read returns 0, io.EOF.
func (f *File) Read(b []byte) (n int, err error) {
if err := f.checkValid("read"); err != nil {
return 0, err
}
n, e := f.read(b)
return n, f.wrapErr("read", e)
}
|
写入
结构体
定义
1
2
3
4
5
6
|
type struct_name struct {
member1 datatype;
member2 datatype;
member3 datatype;
...
}
|
案例:
1
2
3
4
5
6
|
type Person struct {
name string
age int
job string
salary int
}
|
访问
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package main
import ("fmt")
type Person struct {
name string
age int
job string
salary int
}
func main() {
var pers1 Person
var pers2 Person
// Pers1 specification
pers1.name = "Hege"
pers1.age = 45
pers1.job = "Teacher"
pers1.salary = 6000
// Pers2 specification
pers2.name = "Cecilie"
pers2.age = 24
pers2.job = "Marketing"
pers2.salary = 4500
// Access and print Pers1 info
fmt.Println("Name: ", pers1.name)
fmt.Println("Age: ", pers1.age)
fmt.Println("Job: ", pers1.job)
fmt.Println("Salary: ", pers1.salary)
// Access and print Pers2 info
fmt.Println("Name: ", pers2.name)
fmt.Println("Age: ", pers2.age)
fmt.Println("Job: ", pers2.job)
fmt.Println("Salary: ", pers2.salary)
}
|