zoukankan      html  css  js  c++  java
  • 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", "害孩子"))
    }
    
    
  • 相关阅读:
    [LUOGU] P2196 挖地雷
    [LUOGU] P1020 导弹拦截
    [LUOGU] P2543 [AHOI2004]奇怪的字符串
    [LUOGU] P2759 奇怪的函数
    [LUOGU] P1048 采药
    [LUOGU] P1396 营救
    [LUOGU] P1196 [NOI2002]银河英雄传说
    [LUOGU] 2820 局域网
    知识点
    swich使用
  • 原文地址:https://www.cnblogs.com/carp-li/p/15127546.html
Copyright © 2011-2022 走看看