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的更多相关文章

  1. PA教材提纲 TAW12-1

    Unit1 Introduction to Object-Oriented Programming(面向对象编程介绍) 1.1 Explaining the Object-Oriented Progr ...

  2. php基础总结

    目录 PHP开发基础 运算符.表达式和流程控制语句 数组和字符串 函数 PHP与Web页面交互 PHP操作MySQL数据库 面向对象基础 期间看到的几篇有意思的博客 为什么 var_dump(&quo ...

随机推荐

  1. [BOI2003] Gem - 树形dp

    结论 不同颜色数不会超过 \(O(\log n)\) 然后就是很简单的树形dp了 顺便复习一下树形dp怎么写 #include <bits/stdc++.h> using namespac ...

  2. Angular 相关概念

    1.XMLHttpRequest 对象(属于xmlJavascript) XMLHttpRequest 对象用于在后台与服务器交换数据. Ajax 是对XMLHttpRequest 的封装,XMLHt ...

  3. linux上部署springboot应用的脚本

    #!/bin/bash #getProcessId then kill pids=$(ps -ef | grep flashsale| awk '{print $2}') for pid in $pi ...

  4. XSS挑战之旅,学习笔记

    第一关: http://test.ctf8.com/level1.php?name=test 观察到通过get方式传参有会显, 直接打最简单的xss playload: <script>a ...

  5. 使用Opencv3.2出现l error C4996:fopen

    ------ 已启动生成: 项目: test, 配置: Debug x64 ------1> test.cpp1>e:\vs2015opencv\opencv3.2\opencv\buil ...

  6. 0007 settings.py配置文件详解

    01 DEBUG调试配置 开发期设置为True,发布时设置为False 02 INSTALLED_APPS已安装的APP配置 INSTALLED_APPS = [ 'django.contrib.ad ...

  7. jQuery func

    1.  $(selector).each(function(index,element)); -----------index 选择器的index位置,element --当前的元素 2. _.eac ...

  8. normalization, standardization and regularization

    Normalization Normalization refers to rescaling real valued numeric attributes into the range 0 and ...

  9. pytorch save model + Gmatch4py + jupyter debugger + community structure

    1. pytorch, 使用训练好的模型测试自己图片 2. [ pytorch ] ——基本使用:(2) 训练好的模型参数的保存以及调用 3. Gmatch4py 4. Network Analysi ...

  10. 执行PDB的PITR恢复失败的说明

    Oracle 12.1版本中,UNDO表空间仅存在CDB级别(共享UNDO),来自于AskScuti博客园. Oracle 12.2版本开始,UNDO表空间同时可以存在每个PDB级别(本地UNDO). ...