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 ...
随机推荐
- 多项式乘法逆元 - NTT
递归求解即可 #include <bits/stdc++.h> using namespace std; #define int long long namespace NTT { #de ...
- Selenium3+python自动化013-操作浏览器的Cookie
为什么要用Cookie?在测试多个页面时候可绕过验证码输入,直接添加cookie,也可以在添加唯一标识时候使用. 一.操作浏览器的Cookie 1.1.验证码的处理方式 说明:WebDriver类库中 ...
- 码云项目克隆至github
个人博客 地址:http://www.wenhaofan.com/article/20181104211917 因为我的博客项目一开始是存放在码云上面的,但是我又想在GitHub上有该项目的提交记录, ...
- 【做题笔记】洛谷P1506 拯救oibh总部
跑一遍染色法,最后判断哪些位置没被染色即可 一些技巧: 为了判断方便,把字符转换成 int 型的数字. 注意边界问题 详细解释见代码 #include <iostream> #includ ...
- 修改json数据中key(键值)
//方法一:修改JSONObject的键 public static JSONObject changeJsonObj(JSONObject jsonObj,Map<String, String ...
- windows redis启动
1.下载redis 2.启动redis 3.启动redis客户端并设置protected-mode为false
- ModuleNotFoundError: No module named 'numpy.testing.nosetester'
- [codeigniter4]Upgrading from 3.x to 4.x
CodeIgniter 4 is a rewrite of the framework, and is not backwards compatible. It is more appropriate ...
- AcWing 1016. 最大上升子序列和
#include<iostream> using namespace std ; ; int f[N]; int a[N]; int main() { int n; cin>> ...
- 洛谷P1086 花生采摘
https://www.luogu.org/problem/P1086 #include <bits/stdc++.h> using namespace std; typedef long ...