Go_type
1. type的定义和使用
Go语言支持函数式编程,可以使用高阶编程语法。一个函数可以作为另一个函数的参数,也可以作为另一个函数的返回值,那么在定义这个高阶函数的时候,如果函数的类型比较复杂,我们可以使用type来定义这个函数的类型。
byte和rune:
在 Go 1.9 版本之前的内建类型定义的代码是这样写的:
type byte uint8
type rune int32
而在 Go 1.9 版本之后变为:
type byte = uint8 //类型别名的作用就让代码更具有可读性
type rune = int32 //存字符,如果是int32,以为是一个数字
package main import (
"fmt"
"strconv"
) func main() {
/*
type:用于类型定义和类型别名 1.类型定义:type 类型名 Type //自定义type具有隐藏原type的效果
2.类型别名:type 类型名 = Type */
var i1 myint
var i2 = 100 //int
i1 = 200
fmt.Println(i1, i2) //200 100 var name mystr
name = "王二狗"
var s1 string
s1 = "李小花"
fmt.Println(name, s1) //王二狗 李小花 //i1 = i2 //cannot use i2 (type int) as type myint in assignment //name = s1 //cannot use s1 (type string) as type mystr in assignment fmt.Printf("%T,%T,%T,%T\n", i1, i2, name, s1) //main.myint,int,main.mystr,string res1 := fun1()
fmt.Println(res1(10, 20)) //1020 } //1.定义一个新的类型
type myint int
type mystr string //2.定义函数类型
type myfun func(int, int) (string)
//返回值是一个函数时,就不需要写那么复杂了
func fun1() myfun { //fun1()函数的返回值是myfun类型
fun := func(a, b int) string {
s := strconv.Itoa(a) + strconv.Itoa(b)
return s
}
return fun
} //3.类型别名
type myint2 = int //不是重新定义新的数据类型,只是给int起别名,和int可以通用
2. 非本地类型不能定义方法
package main
import "time"
func main() {
}
type MyDuration = time.Duration
//Duration是在time这个包下定义的,现在是在main这个包下修改,是不允许的
//解决方法:
//1.在time这个包下定义
//2.将类型别名改为类型定义: type MyDuration time.Duration,也就是将 MyDuration 从别名改为类型
func (m MyDuration) SimpleSet(){ //cannot define new methods on non-local type time.Duration
}
3. 在结构体成员嵌入时使用别名
package main
import "fmt"
type Person struct {
name string
}
func (p Person) show() {
fmt.Println("Person--->", p.name)
}
//类型别名
type People = Person
func (p People) show2() {
fmt.Println("People--->", p.name)
}
type Student struct {
//嵌入两个结构体
Person
People
}
func main() {
var s Student
//s.name = "王二狗" //ambiguous selector s.name //混淆
s.Person.name = "王二狗"
//s.show() //ambiguous selector s.show
s.Person.show() //Person---> 王二狗
//他们都是Person类型,People只是别名
//虽然是同一类型,但是是两个结构体了
fmt.Printf("%T,%T\n", s.Person, s.People) //main.Person,main.Person
fmt.Printf("%P--%P", &s.Person, &s.People) //&{%!P(string=王二狗)}--&{%!P(string=)}
fmt.Println()
s.People.name = "李小花"
s.People.show() //People---> 李小花
s.People.show2() //People---> 李小花
s.Person.show() //Person---> 王二狗
//s.Person.show2() //没有这个方法
}
Go_type的更多相关文章
- PA教材提纲 TAW12-1
Unit1 Introduction to Object-Oriented Programming(面向对象编程介绍) 1.1 Explaining the Object-Oriented Progr ...
- php基础总结
目录 PHP开发基础 运算符.表达式和流程控制语句 数组和字符串 函数 PHP与Web页面交互 PHP操作MySQL数据库 面向对象基础 期间看到的几篇有意思的博客 为什么 var_dump(&quo ...
随机推荐
- "换行"和"回车"的来历
\r: return 到当前行的最左边. \n: newline 向下移动一行,并不移动左右. Linux中\n表示:回车+换行: Windows中\r\n表示:回车+换行. Mac中\r表示:回车+ ...
- python Threading模块源码解析
查看源码: 这是一个线程控制的类,这个类可以被子类化(继承)在一定的条件限制下,这里有两种方式去明确活动:第一通过传入一个callable 对象也就是调用对象,一种是通过重写这个Thread类的run ...
- webpack之 plugin(插件)
plugin plugin是插件的意思,通常用来对某个现有的架构就行拓展 webpack中的插件,就是对webpack现有功能的各种扩展,比如打包优化,文件压缩等 loader和plugin区别 lo ...
- html5 流式布局 弹式布局 flex
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...
- Codeforces Round #603 (Div. 2) A.Sweet Problem
#include <cstdio> #include <algorithm> using namespace std; int main() { int t; scanf(&q ...
- actiBPM插件的办法
1.下载actiBPM到本地 从IDEA官网下载actiBPM.jar包 IDEA官网:https://plugins.jetbrains.com/ 官网搜索actiBPM 2.从本地安装actiBP ...
- 【C语言】输入一个整数x并判断x是否存在于数组a中
#include<stdio.h> int main() { ] = { ,,,,,,,, };//数组初始化 printf("请输入要查找的数据:\n"); scan ...
- 0121 spring-boot-redis的使用
redis是什么呢?redis,属于NoSQL的一种,在互联网时代,起到加速系统的作用. redis是一种内存数据库,支持7种数据类型的存储,性能1S 10w次读写: redis提供的简单的事务保证了 ...
- 线程同步器CountDownLatch
Java程序有的时候在主线程中会创建多个线程去执行任务,然后在主线程执行完毕之前,把所有线程的任务进行汇总,以前可以用线程的join方法,但是这个方法不够灵活,我们可以使用CountDownLatch ...
- 并发之ATOMIC原子操作--CAS乐观锁原理(二)
1.乐观锁介绍 程序完成并发操作时,访问数据时每次不加锁,假设没有冲突去完成某项操作,如果因为冲突失败就重试,直到成功为止.就是当去做某个修改或其他操作的时候它认为不会有其他线程来做同样的操作(竞争) ...