Go随笔记录

package main

import (
	"fmt"
	"runtime"
	"strconv"
	"time"
)

//主函数
func main() {
	/*输出*/
	fmt.Println("你好!")
	/*======================
	  ======================*/
	/*变量方式1*/
	var num int = 50
	/*变量声明方式2*/
	x := 100
	/*======================
	  ======================*/
	//定义指针、数组与获取存储的值
	var pt int = 10
	var ptr *int = &pt
	//数组
	var arr1 [6]int
	//初始化
	var arr2 [6]int = [6]int{1, 2, 3}
	fmt.Println(arr1[1], arr2[1])
	fmt.Println(num, x, *ptr)

	/*======================
	======================*/
	/*判断*/
	if x > num {
		fmt.Println("x大于num,")
	}
	/*======================
	======================*/
	/*日期时间*/
	now := time.Now()
	/*_ :=*/ now.Year()
	/*_ :=*/ now.Month()
	/*_ :=*/ now.Day()
	date := now.Format("2006-01-02 15:04:05")
	fmt.Println(date)
	/*======================
	======================*/
	/*字符串与数字互转*/
	//字符串转基本整型
	var str string = "520"
	var str1 int = 520
	var str2 int32 = 520
	var str3 int64 = 520
	base, _ := strconv.Atoi(str)
	//字符串转int32
	base32, _ := strconv.ParseInt(str, 10, 32)
	//字符串转int64
	base64, _ := strconv.ParseInt(str, 10, 64)
	fmt.Println("字符串转int:", base, base32, base64)
	//str1转字符
	s1 := strconv.Itoa(str1)
	//str2转字符
	s2 := fmt.Sprint(str2)
	//str3转字符
	s3 := strconv.FormatInt(str3, 10)
	fmt.Println("int转字符串:", s1, s2, s3)
	/*======================
	  ======================*/
	/*控制台输入*/
	var input int
	fmt.Scanln(&input)
	itoa := strconv.Itoa(input)
	fmt.Println("输入为:" + itoa)
	/*======================
	  ======================*/
	/*循环*/
	for i := 0; i < 10; i++ {
		fmt.Println("普通循环:", i)
	}
	/*foreach循环*/
	var vb string = "abcdef"
	for i, v := range vb {
		fmt.Println("foreach循环:", i, v)
	}
	/*======================
	======================*/
	//自定义类型使用
	sy := demo1(demo, "诸葛村夫")
	fmt.Println("自定义类型:", sy)
	/*======================
	  ======================*/
	//defer语句将方法加入栈中,完成后从栈中取出,常用于资源的释放
	defer fmt.Println("defer方法调用完执行")
	/*======================
	======================*/
	//异常的捕获
	errorDemo()
	fmt.Println("异常出现正常运行")
	/*======================
	  ======================*/
	//切片定义
	var slice []int = make([]int, 4)
	s := []int{1, 2, 3}
	fmt.Println("切片:", slice, s)
	/*======================
	  ======================*/
	//map集合定义
	maps := make(map[string]string, 4)
	maps["刘备"] = "蜀国"
	maps["曹操"] = "魏国"
	maps["孙权"] = "吴国"
	for key, val := range maps {
		fmt.Println(key, "===>", val)
	}
	/*======================
	  ======================*/
	//结构体实例化
	stu := Students{}
	stu.Name = "小红"
	stu.Age = 20
	stu.Sex = "男"
	fmt.Println(stu)
	/*======================
	======================*/
	/*接口使用*/
	//接口
	var hones hone
	//小米
	hones = new(xiaomiHone)
	hones.call()
	//苹果
	hones = new(iHone)
	hones.call()

	/*======================
	======================*/
	/*go并发、chan*/
	//创建数组
	g := []int{1, 2, 3, 4, 5, 6} //21
	//创建通道
	ch := make(chan int,2)
	//切片分隔数据
	pre := g[:len(g)/2]
	suffix := g[len(g)/2:]
	go sum(pre, ch)
	go sum(suffix, ch)
	x, y := <-ch, <-ch
	//获取cpu核心数
	cpu := runtime.NumCPU()
	fmt.Println("sum:",x+y)
	fmt.Println("cpu核心数:",cpu)



}

/*自定义类型*/
type myfunc func(name string) string

//方法声明: 方法名(方法参数)返回类型,大写表示其他包可访问
func demo(name string) string {
	return name
}

func demo1(myfunc2 myfunc, c string) string {
	return myfunc2(c)
}

/*初始化函数只执行一次*/
func init() {
	fmt.Println("初始化我被执行")
}

/*异常处理*/
func errorDemo() {
	defer func() {
		err := recover()
		if err != nil {
			fmt.Println("除数不能为0")
		}
	}()
	var i int = 2
	var j int = 0
	j2 := i / j
	fmt.Println(j2)
}

/*结构体定义*/
type Students struct {
	Name string
	Sex  string
	Age  int
}

/*接口手机抽象*/
type hone interface {
	call()
}

//接口具体结构体小米手机
type xiaomiHone struct {
	//名字
	Name string
	//价格
	Price float64
}

//小米手机结构体方法实现抽象接口方法
func (xihone *xiaomiHone) call() {
	(*xihone).Name = "小米手机"
	xihone.Price = 3200.00
	fmt.Println("小米手机", xihone)
}

//接口具体结构体苹果手机
type iHone struct {
	//名字
	Name string
	//价格
	Price float64
}

//苹果手机结构体方法实现抽象接口方法
func (ih *iHone) call() {
	(*ih).Name = "苹果手机"
	ih.Price = 6200.00
	fmt.Println("苹果手机", ih)
}

/*go并发、chan*/
func sum(count []int, ch chan int) {
	sum := 0
	//求和
	for _, v := range count {
		sum += v
	}
	//将sum发送给ch通道
	ch <- sum
}

posted @ 2021-10-04 17:49  猿大佛  阅读(15)  评论(0编辑  收藏  举报