minikube代码分析与Go语言 - 2
openqt · · 1019 次点击 · · 开始浏览[TOC]
Go语言简介
2 - 基本要素
Printf参数备忘:
| 符号 | 解释 |
|---|---|
| %d | decimal integer |
| %x, %o, %b | integer in hexadecimal, octal, binary |
| %f, %g, %e | floating-point number: 3.141593, 3.141592653589793, 3.141593e+00 |
| %t | boolean: true or false |
| %c | rune (Unicode code point) |
| %s | string |
| %q | quoted string "abc" or rune 'c' |
| %v | any value in a natural format |
| %T | type of any value |
| %% | literal percent sign (no operand) |
命名
命名规则跟C相似。
Go有25个关键字不能用作名字:
Go还有37个预声明的名字用于内置常量、类型和函数(可覆盖):
Constants: true, false, iota, nil
Types: int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex64 complex128
bool byte rune string error
Functions: make len cap new append copy close delete
complex real imag
panic recover
名字第一个字符决定其可见性。大写字符表示对外公开。包名始终是小写字符。
名字长度没有限制,但倾向于较短的名字,命名使用驼峰风格。
变量声明可以按列表方式,如
var b, f, s = true, 2.3, "four"支持
tuple assignment,如i, j = j, i // swap values of i and j简化的声明形式必须要声明至少一个新变量
var i, j int; i, j := 1, 2 // compile error: no new variables指针与
C类似,*取值&取地址,如*p++
++ec2.go++ (增加命令行参数)
package main
import ( // 导入多个包的常用形式
"fmt"
"flag"
"strings"
)
var n = flag.Bool("n", false, "omit trailing newline")
var sep = flag.String("sep", " ", "separator")
func main() {
flag.Parse()
fmt.Print(strings.Join(flag.Args(), *sep))
if !*n { fmt.Println() }
}
要点
new和delete与C++相似,但它们是函数变量生命周期与
C或Java等主流语言一致,new分配的对象可能位于栈中隐式赋值如
medals := []string{"bronze", "silver", "gold"}type声明一个新的类型,不改变其值,支持其基础类型的操作包机制类似
Java,包对应文件的路径包也体现了命名空间
namespace,类似Python首字符大写即是导出的实体(如变量、函数、类型等等)
跟着包声明的的注释作为包的文档描述,跟
Python有相似之处引入没有用到的包会导致编译错误
func init() { /* ... */ }是文件/包的初始化函数作用域范围跟其它语言相似,但是由于
Go语法的原因有一些奇怪的用法
if f, err := os.Open(fname); err != nil { // compile error: unused: f
return err
}
f.ReadByte() // compile error: undefined f
var cwd string
func init() {
cwd, err := os.Getwd() // compile error: unused: cwd
if err != nil {
log.Fatalf("os.Getwd failed: %v", err)
}
}
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
[TOC]
Go语言简介
2 - 基本要素
Printf参数备忘:
| 符号 | 解释 |
|---|---|
| %d | decimal integer |
| %x, %o, %b | integer in hexadecimal, octal, binary |
| %f, %g, %e | floating-point number: 3.141593, 3.141592653589793, 3.141593e+00 |
| %t | boolean: true or false |
| %c | rune (Unicode code point) |
| %s | string |
| %q | quoted string "abc" or rune 'c' |
| %v | any value in a natural format |
| %T | type of any value |
| %% | literal percent sign (no operand) |
命名
命名规则跟C相似。
Go有25个关键字不能用作名字:
Go还有37个预声明的名字用于内置常量、类型和函数(可覆盖):
Constants: true, false, iota, nil
Types: int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex64 complex128
bool byte rune string error
Functions: make len cap new append copy close delete
complex real imag
panic recover
名字第一个字符决定其可见性。大写字符表示对外公开。包名始终是小写字符。
名字长度没有限制,但倾向于较短的名字,命名使用驼峰风格。
变量声明可以按列表方式,如
var b, f, s = true, 2.3, "four"支持
tuple assignment,如i, j = j, i // swap values of i and j简化的声明形式必须要声明至少一个新变量
var i, j int; i, j := 1, 2 // compile error: no new variables指针与
C类似,*取值&取地址,如*p++
++ec2.go++ (增加命令行参数)
package main
import ( // 导入多个包的常用形式
"fmt"
"flag"
"strings"
)
var n = flag.Bool("n", false, "omit trailing newline")
var sep = flag.String("sep", " ", "separator")
func main() {
flag.Parse()
fmt.Print(strings.Join(flag.Args(), *sep))
if !*n { fmt.Println() }
}
要点
new和delete与C++相似,但它们是函数变量生命周期与
C或Java等主流语言一致,new分配的对象可能位于栈中隐式赋值如
medals := []string{"bronze", "silver", "gold"}type声明一个新的类型,不改变其值,支持其基础类型的操作包机制类似
Java,包对应文件的路径包也体现了命名空间
namespace,类似Python首字符大写即是导出的实体(如变量、函数、类型等等)
跟着包声明的的注释作为包的文档描述,跟
Python有相似之处引入没有用到的包会导致编译错误
func init() { /* ... */ }是文件/包的初始化函数作用域范围跟其它语言相似,但是由于
Go语法的原因有一些奇怪的用法
if f, err := os.Open(fname); err != nil { // compile error: unused: f
return err
}
f.ReadByte() // compile error: undefined f
var cwd string
func init() {
cwd, err := os.Getwd() // compile error: unused: cwd
if err != nil {
log.Fatalf("os.Getwd failed: %v", err)
}
}