提交 ba9b1ccf 编写于 作者: 彭世瑜's avatar 彭世瑜

fix

上级 043ad7d0
package main
import (
"fmt"
)
type Phone interface {
call()
}
type NokiaPhone struct {
}
func (nokiaPhone NokiaPhone) call() {
fmt.Println("I am Nokia, I can call you!")
}
type IPhone struct {
}
func (iPhone IPhone) call() {
fmt.Println("I am iPhone, I can call you!")
}
func main() {
var phone Phone
phone = new(NokiaPhone)
phone.call()
phone = new(IPhone)
phone.call()
}
module code
go 1.19
require github.com/satori/go.uuid v1.2.0 // indirect
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
# 第七章 Golang 常量
常量: 程序`编译阶段`就确定下来的值,程序`运行时` 无法改变该值
## 定义常量
```go
// 定义的时候就要初始化
const constantName [type] = value
```
eg:
```go
const PI float32 = 3.14
// 简写
const PI = 3.14
// 批量定义
const (
WIDTH = 200
HEIGHT = 300
)
const WIDTH, HEIGHT = 200, 300
```
## iota
可以被编译器修改的常量
默认值是0,每调用一次加1,遇到const关键字时被重置为0
```go
const (
A = iota // 0
B = iota // 1
)
```
使用下划线(_)跳过某些值
```go
const (
A = iota // 0
_ // 1
B = iota // 2
)
```
中间插队
```go
const (
A = iota // 0
B = 100
C = iota // 2
)
```
# 第五章 Golang标识符、关键字、命名规则
标识符 identifier
- 数字、字母、下划线(_)
- 只能是字符和下划线(_)开头
- 标识符区分大小写
eg:
```go
var name string
var age int
var _sys int
```
关键字25个
```
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
```
36 个预定义标识符
```
append
bool false true
nil
make new
byte
cap
close
copy
complex complex64 complex128
float32 float64
imag real
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64
iota len panic
print println
recover
string
uintptr
```
命名规则
- 包名称:和目录保持一致、小写
- 文件名:小写下划线
- 结构体:大驼峰命名法
- 接口命名:大驼峰命名法,单个函数以`er` 结尾
- 变量命名:驼峰命名法,
- 大写字母开头: 公有
- 小写字母开头: 私有
- bool类型:has/is/can/allow
- 常量:大写下划线
- 单元测试:`*_test.go`
- 测试用例:`Test*`
错误处理
```go
if err != nill {
// 错误处理
return
}
```
# 第二章 Golang 快速开始和特性
# 第二章 Golang 环境安装
## Go语言开发工具
......@@ -38,6 +38,17 @@ vscode快捷键:
- 整体向右移动 tab
- 整体向左移动 shift + tab
快捷输入
```go
pkgm
ff
for
forr
fmain
a.print
```
安装插件
- [https://marketplace.visualstudio.com/items?itemName=golang.Go](https://marketplace.visualstudio.com/items?itemName=golang.Go)
......@@ -178,207 +189,3 @@ demo demo.go
$ ./demo
Hello Golang
```
## Golang执行流程
```bash
# go build
.go 源文件 --> 【编译】 --> 可执行文件 --> 【运行】 --> 结果
# go run
.go 源文件 --> 【编译运行】 --> 结果
```
两种执行流程的区别
- 源文件需要在Go开发环境中才能运行
- 编译后生成可执行文件,可以在没有Go开发环境的机器上运行
- 编译时,会将依赖的库文件包含到可执行文件中,所以,可执行文件比源文件大很多
## 编译运行
- 编译器将go源文件编译成可识别的二进制文件
1、编译
```bash
$ ls
demo.go
# 直接编译文件
$ go build demo.go
$ ls
demo demo.go
# 指定生成的二进制文件名
$ go build -o run demo.go
# windows 下需要制定后缀名 .exe
$ go build -o run.exe demo.go
$ ls
demo demo.go run
```
2、运行
```bash
# 运行可执行文件
./demo
# 直接运行go源文件
go run demo.go
```
## Go语言开发的特点
- 源文件以`.go` 为扩展名
- Go应用程序执行入口是`main()` 方法
- Go语言严格区分大小写
- GO语句后不需要分号,一行写一条语句
- Go语言`定义的变量` 或者`import的包` 没有使用到,代码不能编译通过
- 大括号成对出现
## Golang 转义字符(escape char)
| 转义字符 | 说明
| - | -
| `\t`| 制表位
| `\n` | 换行符,光标移动到下一行
| `\\` | 斜杆
| `\"` | 引号
| `\r` | 回车,光标移动到行首
示例
```go
package main
import "fmt"
func main(){
fmt.Println("Hello\tGolang")
// Hello Golang
fmt.Println("Hello\nGolang")
// Hello
// Golang
fmt.Println("Hello\rGolang")
// Golang
fmt.Println("Hello\\Golang")
// Hello\Golang
fmt.Println("Hello\"Golang")
// Hello"Golang
}
```
示例:使用一行语句输出类似表格布局的数据
```go
package main
import "fmt"
func main(){
fmt.Println("姓名\t年龄\t籍贯\t住址\nTom\t24\t河北\t北京")
// 姓名 年龄 籍贯 住址
// Tom 24 河北 北京
}
```
## 注释 comment
提高代码可读性
```go
// 行注释
/* 块注释 */
```
推荐使用:行注释
## 代码风格
gofmt 格式化
```bash
# 格式化并写入原文件
$ gofmt -w demo.go
```
- 正确的缩进空格
- 函数的左大括号,放在函数名后面(不换行)
- 运算符两边加空格
- 每行代码字符不超过80个字
## Golang 标准库API
[https://studygolang.com/pkgdoc](https://studygolang.com/pkgdoc)
```
包 <==> 文件夹 / 原文件 / 函数
```
## Dos 基本介绍
Dos Disk Operating System 磁盘操作系统
基本原理
```
终端 --> Dos操作系统 --> Windows目录
```
## Go常用命令
```bash
$ go help
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
work workspace maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
```
管理依赖包
搜索包:https://pkg.go.dev
```bash
# 初始化模块
go mod init code
# 下载依赖
go get github.com/go-sql-driver/mysql
```
快捷输入
```go
pkgm
ff
for
forr
fmain
a.print
```
\ No newline at end of file
# 第四章 Golang 项目管理
## go项目管理工具
- version < golang1.11: gopath
- version >= golang1.11:gomod
- 第三方:govendor
# 编写go代码
```bash
# 初始化项目
go mod init <projectName>
# eg:
$ go mod init demo
$ tree
.
├── go.mod
├── main.go
└── user
└── user.go
```
main.go
```go
package main
import (
"demo/user"
"fmt"
)
func main() {
// user.SayHello().var
s := user.SayHello()
// s.print
fmt.Println(s)
}
```
user/user.go
```go
package user
func SayHello() string {
return "Hello"
}
```
运行
```go
go run main.go
```
# 第三章 Golang 运行代码
## Golang执行流程
```bash
# go build
.go 源文件 --> 【编译】 --> 可执行文件 --> 【运行】 --> 结果
# go run
.go 源文件 --> 【编译运行】 --> 结果
```
两种执行流程的区别
- 源文件需要在Go开发环境中才能运行
- 编译后生成可执行文件,可以在没有Go开发环境的机器上运行
- 编译时,会将依赖的库文件包含到可执行文件中,所以,可执行文件比源文件大很多
## 编译运行
- 编译器将go源文件编译成可识别的二进制文件
1、编译
```bash
$ ls
demo.go
# 直接编译文件
$ go build demo.go
$ ls
demo demo.go
# 指定生成的二进制文件名
$ go build -o run demo.go
# windows 下需要制定后缀名 .exe
$ go build -o run.exe demo.go
$ ls
demo demo.go run
```
2、运行
```bash
# 运行可执行文件
./demo
# 直接运行go源文件
go run demo.go
```
## Go语言开发的特点
- 源文件以`.go` 为扩展名
- Go应用程序执行入口是`main()` 方法
- Go语言严格区分大小写
- GO语句后不需要分号,一行写一条语句
- Go语言`定义的变量` 或者`import的包` 没有使用到,代码不能编译通过
- 大括号成对出现
## Golang 转义字符(escape char)
| 转义字符 | 说明
| - | -
| `\t`| 制表位
| `\n` | 换行符,光标移动到下一行
| `\\` | 斜杆
| `\"` | 引号
| `\r` | 回车,光标移动到行首
示例
```go
package main
import "fmt"
func main(){
fmt.Println("Hello\tGolang")
// Hello Golang
fmt.Println("Hello\nGolang")
// Hello
// Golang
fmt.Println("Hello\rGolang")
// Golang
fmt.Println("Hello\\Golang")
// Hello\Golang
fmt.Println("Hello\"Golang")
// Hello"Golang
}
```
示例:使用一行语句输出类似表格布局的数据
```go
package main
import "fmt"
func main(){
fmt.Println("姓名\t年龄\t籍贯\t住址\nTom\t24\t河北\t北京")
// 姓名 年龄 籍贯 住址
// Tom 24 河北 北京
}
```
## 注释 comment
提高代码可读性
```go
// 行注释
/* 块注释 */
```
推荐使用:行注释
## 代码风格
gofmt 格式化
```bash
# 格式化并写入原文件
$ gofmt -w demo.go
```
- 正确的缩进空格
- 函数的左大括号,放在函数名后面(不换行)
- 运算符两边加空格
- 每行代码字符不超过80个字
## Golang 标准库API
[https://studygolang.com/pkgdoc](https://studygolang.com/pkgdoc)
```
包 <==> 文件夹 / 原文件 / 函数
```
## Dos 基本介绍
Dos Disk Operating System 磁盘操作系统
基本原理
```
终端 --> Dos操作系统 --> Windows目录
```
## Go常用命令
```bash
$ go help
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
work workspace maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
```
管理依赖包
搜索包:https://pkg.go.dev
```bash
# 初始化模块
go mod init code
# 下载依赖
go get github.com/go-sql-driver/mysql
```
\ No newline at end of file
# 第六章 Golang变量
变量是计算机语言中能够存储计算结果或能表示值的抽象概念,不同的变量保存的数据类型可能不一样
## 声明变量
go语言中的变量需要声明后再使用,且必须使用
语法
```go
var identifier type
```
eg:
```go
var name string
var age int
var isMan bool
```
批量声明
```go
var (
name string
age int
isMan bool
)
```
## 变量的初始化
语法
```go
var 变量名 类型 = 表达式
```
eg:
```go
var name string = "Tom"
var age int = 20
```
类型推断: 可以省略类型
```go
var name = "Tom"
var age = 20
```
初始化多个变量
```go
var name, age = "Tom", 23
```
## 短变量声明
`函数内部` 使用`:=` 运算符进行声明变量
```go
func main() {
name := "Tom"
age := 20
}
```
## 匿名变量
```go
func getNameAndAge() (string, int){
return "Tom", 20
}
func main(){
name, age := getNameAndAge()
// 使用下划线丢弃
name, _ := getNameAndAge()
}
```
\ No newline at end of file
......@@ -10,9 +10,19 @@
[第一章 Golang 概述](blog/golang/golang-start.md)
[第二章 Golang 快速开始和特性](blog/golang/golang-install.md)
[第二章 Golang 环境安装](blog/golang/golang-install.md)
[第三章 Golang 运行代码](blog/golang/golang-run.md)
[第四章 Golang 项目管理](blog/golang/golang-project.md)
[第五章 Golang标识符、关键字、命名规则](blog/golang/golang-identifier.md)
[第六章 Golang变量](blog/golang/golang-variable.md)
[第七章 Golang 常量](blog/golang/golang-constant.md)
https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
https://www.bilibili.com/video/BV1zR4y1t7Wj?p=8&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
https://www.bilibili.com/video/BV1zR4y1t7Wj?p=12&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
......@@ -124,6 +124,8 @@ Javascript HTML5 canvas library
[西瓜视频播放器(HTML5)](https://v2.h5player.bytedance.com/) 一款带解析器、能节省流量的HTML5视频播放器
[https://prettyhtml.netlify.app/](https://prettyhtml.netlify.app/):[](https://github.com/Prettyhtml/prettyhtml)
## Node.js
https://npm.devtool.tech/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册