• Go_01_自定义类型&类型别名


    package main
    
    import "fmt"
    
    // 1、定义类型别名
    // 类似Linux的alias命令,只是为了代码可读性或者简写
    // go语言编译的时候并不会产生新的类型
    type str = string
    
    // 2、自定义类型
    // 产生一个新的类型,不破坏已有的类型,并可以为之扩充方法
    type Dict map[string]interface{}
    
    // 为自定义Dict类型添加一个push方法
    func (d Dict) PushItem(key str, value interface{}) {
    	d[key] = value
    }
    
    // 为自定义Dict类型添加一个get方法
    func (d Dict) GetDefault(key str, defaultValue interface{}) interface{} {
    	if value, ok := d[key]; ok {
    		return value
    	}
    	return defaultValue
    }
    
    // 3、自定义函数类型 相同类型个数参数,相同类型返回值 就是同一个函数类型
    type HandleFunc func(dict *Dict) str
    
    func main() {
    	var name str
    	dict := Dict{}
    	dict.PushItem("name", "lzh")
    	fmt.Printf("%T", dict)
    	fmt.Printf("%T", name)
    	fmt.Println(dict.GetDefault("lzh", "害孩子"))
    }
    
    
  • 相关阅读:
    poj 1634
    poj 2153
    POJ 1693
    poj 1789
    POJ 2676
    vue 路由
    用 node.js 创建第一个Hello World
    js原生Ajax 的封装和原理
    BFC原理
    怎么理解js的面向对象编程
  • 原文地址:https://www.cnblogs.com/carp-li/p/15127546.html
Copyright © 2020-2023  润新知