golang特点
代码语言:txt复制goroutine:性能极高(占用内存小,并发数多) 运行时直接打包代码:不依赖任何环境即可执行Hello World
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
variable
代码语言:txt复制package main
import "fmt"
func main() {
var username, sex string
username = "张鹏"
sex = "男"
fmt.Println(username)
fmt.Println(sex)
// 方法一
var num int = 100
fmt.Println(num)
// 方法二
var num2 int
num2 = 200
fmt.Println(num2)
num3 := 300
fmt.Println(num3)
}
variable multiple
代码语言:txt复制package main
import "fmt"
func main() {
var username, sex string
username = "张鹏"
sex = "男"
fmt.Println(username)
fmt.Println(sex)
// 方法一
var num int = 100
fmt.Println(num)
// 方法二
var num2 int
num2 = 200
fmt.Println(num2)
num3 := 300
fmt.Println(num3)
}
constant
代码语言:txt复制package main
import "fmt"
func main() {
const const_num = 100
fmt.Println("这是一个常量:", const_num)
// 变量1
var num1 int
num1 = 100
// 变量2
num2 := 200
fmt.Println("这是一个变量:", num1, num2)
}
[zhangpeng@27ops constant]$ go run const.go
这是常量: 100
这是变量: 200
[zhangpeng@27ops constant]$
init and main
代码语言:txt复制package main
import "fmt"
var log string
// go语言执行的函数入口,所有函数运行都是从main函数开始的
func main() {
fmt.Println("this is main function")
}
func init() {
log = "log对象"
fmt.Println("this is init function", log)
}
[zhangpeng@27ops init_main]$ go run init_main.go
this is init function
this is main function
[zhangpeng@27ops init_main]
call from main function
代码语言:txt复制package main
import "fmt"
var init_func string
var main_func string
func main() {
main_func = "main function"
fmt.Println("1th function,this is", main_func)
sayhi()
}
func init() {
init_func = "init function"
fmt.Println("2th function,this is", init_func)
}
func sayhi() {
fmt.Println("3th function,call from main function")
}
[zhangpeng@27ops init_main]$ go run init_main.go
2th function,this is init function
1th function,this is main function
3th function,call from main function
[zhangpeng@27ops init_main]$
call from init function
代码语言:txt复制package main
import "fmt"
var init_func string
var main_func string
func main() {
main_func = "main function"
fmt.Println("1th function,this is", main_func)
sayhi()
}
func init() {
init_func = "init function"
fmt.Println("2th function,this is", init_func)
sayhi2()
}
func sayhi() {
fmt.Println("3th function,call from main function")
}
func sayhi2() {
fmt.Println("4th function,call from init function")
}
[zhangpeng@27ops init_main]$ go run init_main.go
2th function,this is init function
4th function,call from init function
1th function,this is main function
3th function,call from main function
[zhangpeng@27ops init_main]$
type of int
代码语言:txt复制package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
// 整型数值
var (
a1 int
b1 int8
c1 int16
a2 int32
b2 int64
)
a1 = 4
b1 = 100
a2 = 1000
b2 = 1000
fmt.Println(a1)
fmt.Println(b1)
fmt.Println(c1)
fmt.Println(a2)
fmt.Println(b2)
// 查看数据类型方法一:reflect.TypeOf(a)
fmt.Println(reflect.TypeOf(a1))
// 查看数据类型方法二:使用printf %T类型 %v值
// %s 打印字符串 %d打印数值
fmt.Printf("a: %T %v n", a1, a1)
// 打印字节数
fmt.Println("字节长度", unsafe.Sizeof(b2))
}
C:UsersAdministratorAppDataLocalTempGoLand___go_build_numbers_go.exe
4
100
0
1000
1000
int
a: int 4
字节长度 8
Process finished with the exit code 0
type of bool
代码语言:txt复制package main
import (
"fmt"
"unsafe"
)
func main() {
br := true
var b3 = false
fmt.Println(br, b3)
// 打印字节数
fmt.Println(unsafe.Sizeof(br))
}
C:UsersAdministratorAppDataLocalTempGoLand___go_build_data_bool_go.exe
true false
1
Process finished with the exit code 0
string
代码语言:txt复制// study string
package main
import "fmt"
func main() {
s1 := "单个字符串"
s2 := `
第一个字符串
第二个字符串
第三个字符串
`
fmt.Println(s1)
fmt.Println(s2)
}
[zhangpeng@27ops string]$ go run string_demo.go
单个字符串
第一个字符串
第二个字符串
第三个字符串
[zhangpeng@27ops string]$
slice
代码语言:txt复制[zhangpeng@27ops slice]$ cat string_slice_for.go
package main
import "fmt"
func main() {
v := []string{"bj", "sh", "sz"}
for i := 0; i < len(v); i {
//fmt.Println(i)
fmt.Println(i,v[i])
}
}
[zhangpeng@27ops slice]$
[zhangpeng@27ops slice]$ go run string_slice_for.go
0 bj
1 sh
2 sz
[zhangpeng@27ops slice]$
map
代码语言:txt复制package main
import "fmt"
func main() {
/*
map是一种无序的基于key-value的数据结构
定义一个map字典
*/
// [string]:表示key的数据类型
// string:表示value值的数据类型
userInfo := map[string]string{
"张鹏": "18",
"性别": "男",
}
fmt.Println(userInfo)
/*
判断一个key是否在map中
*/
val, ok := userInfo["张鹏"]
fmt.Println(val, ok)
val, user := userInfo["zhangpeng"]
fmt.Println(val, user)
}