/*
* @Author: hupc
* @Date: 2020-09-22 13:53:45
* @LastEditTime: 2021-04-02 15:05:42
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \go_test\main.go
*/
package main
import (
"fmt"
"math"
"math/rand"
"sort"
"strings"
"time"
)
type Profile struct {
name string
age int
gender string
mother *Profile
father *Profile
}
func (person Profile) FmtProfile() {
fmt.Printf("姓名:%s ", person.name)
fmt.Printf("年龄:%d ", person.age)
fmt.Printf("性别:%s ", person.gender)
}
func (person *Profile) increase_age() {
person.age += 1
}
func set_data(x int) {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
var arr [10]int
arr[x] = 88
}
func test_type_assertion() {
var i interface{} = 10
fmt.Println("i: ", i)
t1 := i.(int)
fmt.Println("t1: ", t1)
fmt.Println("=======cutting line=========")
t2 := i.(string)
fmt.Println("t2: ", t2)
}
func test_type_assertion2() {
fmt.Println("===========example 1==============")
var i interface{} = 10
t1, ok := i.(int)
fmt.Printf("t1: %d - ok: %t\n", t1, ok)
fmt.Println("===========example 2==============")
t2, ok := i.(string)
fmt.Printf("t2: %s - ok: %t\n", t2, ok)
fmt.Println("===========example 3==============")
var k interface{}
t3, ok := k.(interface{})
fmt.Printf("t3: %s - ok: %t\n", t3, ok)
fmt.Println("===========example 4==============")
k = 10
t4, ok := k.(interface{})
fmt.Printf("t4: %d - ok: %t\n", t4, ok)
fmt.Println("===========example 5==============")
t5, ok := k.(int)
fmt.Printf("t5: %d - ok: %t\n", t5, ok)
}
func foo() (int, string) {
return 10, "hupc"
}
// go 变量
// 变量名不能重复
// 函数外生命变量必须 var
// 变量声明时如果没有初始值 必须要有类型 如果声明时赋值了初始值 则不用声明类型 go会自己根据 = 右边的数据来判断变量类型
// 函数内部生命变量 一般使用:=
// _ 是匿名变量
//
//
// go string
// go string 的len 方法 求出来的长度是字符串的 字节长度 如果是字母则直接可以求出 字符长度 如果是中文则不行
// 处理中文时要用到 rune 类型 是将中文等复合字符转换成Unicode来处理
func changeString() {
s1 := "big"
byteS1 := []byte(s1)
byteS1[0] = 'p'
fmt.Println(s1, string(byteS1))
s2 := "红萝卜"
byteS2 := []rune(s2)
byteS2[0] = '白'
fmt.Println(s2, byteS2, string(byteS2))
}
// go 类型转换
// go中只有强制类型转换 没有隐式类型转换
// 通过 T() 方式来转换 T 是要转换的类型 ex: string() int() byte()
func sqrtDemo() {
a, b := 3, 4
var c int
//math.Sqrt()接收的参数是float64类型 需要强制转换
c = int(math.Sqrt(float64(a*b + b*b)))
fmt.Println(c)
}
func test() {
var (
a int = 3
b float32 = 3.2
c bool = true
d string = "abc"
)
fmt.Printf("%v(%T) %vf(%T) %v(%T) %v(%T)", a, a, b, b, c, c, d, d)
fmt.Println()
s := "hello沙河小王子"
byteA := []rune(s)
fmt.Println(byteA)
var count int
for _, v := range byteA {
if len(string(v)) >= 3 {
count++
}
}
fmt.Println("共有中文", count, "个")
fmt.Printf("共有中文 %d 个", count)
}
// 1x1
// 1x2 2x2
// 1x3 2x3 3x3
//
func multi_table_99() {
for i := 1; i < 10; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%dx%d=%d ", j, i, i*j)
}
fmt.Println()
}
}
//求数组[1, 3, 5, 7, 8]所有元素的和
func sumArray(x []int) []int {
var count int = 0
for _, v := range x {
count += int(v)
}
x[0] = 111
return x
}
func sortMap() {
rand.Seed(time.Now().UnixNano())
var scoreMap = make(map[string]int, 20)
for i := 0; i < 10; i++ {
key := fmt.Sprintf("stu%02d", i)
value := rand.Intn(10)
scoreMap[key] = value
}
//取出map中所有的key 存入切片
var keys = make([]string, 0, 20)
for key := range scoreMap {
keys = append(keys, key)
}
//对切片进行排序
sort.Strings(keys)
fmt.Println(keys)
fmt.Println(scoreMap)
//按照排序收的key遍历map
for _, kk := range keys {
fmt.Println(kk, scoreMap[kk])
}
}
func mapSliceTest() {
var mapSlice = make([]map[string]string, 3)
for index, value := range mapSlice {
fmt.Println(index, value)
}
fmt.Println("after init")
//对切片中的map元素 进行初始化
// mapSlice[0] = make(map[string]string, 3)
// mapSlice[0]["name"] = "hupc"
// mapSlice[0]["password"] = "123456"
// mapSlice[0]["address"] = "北京"
map1 := make(map[string]string, 3)
map1["name"] = "hupc"
map1["password"] = "123456"
map1["address"] = "北京"
mapSlice[0] = map1
for index, value := range mapSlice {
fmt.Println(index, value)
}
}
func sliceMapTest() {
var sliceMap = make(map[string][]string, 3)
fmt.Println(sliceMap)
fmt.Println("after init")
key := "中国"
value, ok := sliceMap[key]
if !ok {
value = make([]string, 0, 2)
}
value = append(value, "北京", "上海")
sliceMap[key] = value
key2 := "美国"
value2, ok := sliceMap[key2]
if !ok {
value2 = make([]string, 0, 2)
}
value2 = append(value2, "华盛顿", "洛杉矶")
sliceMap[key2] = value2
fmt.Println(sliceMap)
}
func f1() int {
x := 5
defer func() {
x++
}()
return x
}
func f2() (x int) {
defer func() {
x++
}()
return 5
}
func f3() (y int) {
x := 5
defer func() {
x++
}()
return x
}
func f4() (x int) {
defer func(x int) {
x++
}(x)
return 5
}
func funcA() {
fmt.Println("func A")
}
func funcB() {
defer func() {
err := recover()
fmt.Println("err", err)
if err != nil {
fmt.Println("recover in B")
}
}()
panic("panic in B")
}
func funcC() {
fmt.Println("func in C")
}
func assinCoinByName(name string) int {
if name == "" {
return 0
}
lowerName := strings.ToLower(name)
var count = 0
if finde := strings.Count(lowerName, "e"); finde > 0 {
fmt.Println(name, "find e is", finde)
count += 1
}
if findi := strings.Count(lowerName, "i"); findi > 0 {
fmt.Println(name, "find i is", findi)
count += 2
}
if findo := strings.Count(lowerName, "o"); findo > 0 {
fmt.Println(name, "find o is", findo)
count += 3
}
if findu := strings.Count(lowerName, "u"); findu > 0 {
fmt.Println(name, "find u is", findu)
count += 4
}
return count
}
func dispatchCoin() {
var (
coins = 50
users = []string{
"Matthew", "Sarah", "Augustus", "Heidi", "Emilie", "Peter", "Giana", "Adriano", "Aaron", "Elizabeth",
}
distribution = make(map[string]int, len(users))
totalCount = 0
)
fmt.Println(distribution)
for _, v := range users {
singleCount := assinCoinByName(v)
totalCount += singleCount
distribution[v] = singleCount
}
fmt.Println(distribution)
leftCount := coins - totalCount
fmt.Println("剩下:", leftCount)
}
func main() {
dispatchCoin()
// funcA()
// funcB()
// funcC()
// fmt.Println(f1())
// fmt.Println(f2())
// fmt.Println(f3())
// fmt.Println(f4())
// sliceMapTest()
// mapSliceTest()
// sortMap()
// var a = make([]string, 5, 10)
// for i := 0; i < 10; i++ {
// a = append(a, fmt.Sprintf("%v", i))
// }
// fmt.Println(a)
// var count int
// a := []int{1, 3, 5, 7, 8}
// b := []int{}
// fmt.Println(a)
// fmt.Printf("a is %T", a)
// fmt.Println(b)
// fmt.Printf("b is %T", b)
// var count = sumArray(a)
// fmt.Println(count)
// fmt.Println(a)
// test()
// sqrtDemo()
// changeString()
// s := "hello沙河"
// l := len(s)
// fmt.Println(l)
// a := '中'
// b := '国'
// c := a + b
// fmt.Println(a)
// s1 := `1
// 2
// 3
// 4
// 5
// 6
// 7
//
// `
// fmt.Println(s1)
// fmt.Println("str := \"c:\\Code\\lesson1\\go.exe\"")
// n := 10
// m := 20
// x, _ := foo()
// _, y := foo()
// fmt.Println(x, y)
// set_data(20)
// test_type_assertion2()
// myself := Profile{name: "hupc", age: 27, gender: "male"}
// myself.FmtProfile()
// fmt.Printf("\nhupc当前年龄:%d", myself.age)
// myself.increase_age()
// fmt.Printf("\nhupc明年的年龄:%d", myself.age)
// fmt.Println("\nhello world!")
}