Workspace
  • Introduction
  • Algorithm
    • 二叉树
    • 排序算法
  • Basic Knowledge
    • CAP定理
    • CAS-SSO-单点登陆
    • 单向认证-双向认证
  • CICD
  • Cloud Native
  • Docker
    • Docker特性
    • Docker资源隔离
  • Golang
    • Standard Library
      • Archive
        • Builtin
        • Zip
    • Golang-container包
    • Golang-fallthrough关键字
    • Golang For Slect
    • Golang-Goroutine泄露
    • Golang Interface
    • Golang-json.Unmarshal
    • Golang Label
    • Golang Map String Struct
    • Golang Map To Struct
    • Golang Override Package Function
    • Golang-Slice删除元素
    • Golang Switch
    • Golang-sync.Cond
    • Golang-sync.Map
    • Golang-sync.once
    • Golang-type关键字
    • Golang-代码生成
    • golang-并发数控制
    • Golang-并发退出
    • Golang-插件系统
    • Golang-继承
    • Golang之channel
    • Golang之continue
    • Golang之make与new和nil
    • Golang之map
    • Golang之reflect
    • Golang之类型判断
    • Golang代码质量检测
    • Golang变量避坑
    • Golang字符串遍历
    • golang并发控制代码示例
    • Golang性能优化
    • Golang死锁
    • goroutine-协程-线程-进程
    • go值传递
    • go内存逃逸分析
    • go并发MGP模式
    • go并发控制
    • 垃圾回收-三色法
  • Istio
    • 服务网格
  • Jenkins
    • Jenkin On K 8 S
    • Jenkins Mac
  • Kubernetes
    • Deployment
    • k8s容器内查看-cpu-memory分配情况
    • kube-proxy原理
    • Kubernetes Informers
    • Kubernetes扩展点
    • Kubernetes部署策略
    • Pod Non Root
    • Pod驱逐
    • PV PVC Storage Class
    • Security Context
    • 优雅热更新
  • Python
    • Python-vs-Golang协程区别
  • Serviceless
  • Shell
    • Shell小技巧
  • VPN
    • OC Serv
  • Redis
Powered by GitBook
On this page
  • 定义结构体
  • 定义接口
  • 定义类型
  • 别名定义
  • 定义函数类型
  • 参考

Was this helpful?

  1. Golang

Golang-type关键字

type 在 Golang 中非常常见的关键字,一般的用法就是定义结构,接口等。 但是 type 还有很多其它的用法。

定义结构体

type Person struct {
    name string
    age string
}

type Mutex struct {}

type otherMutex Mutex

定义接口

type Person interface {
    ShowName(s string)
}

定义类型

type Myint int
type (m Myint) showValue() {
    fmt.Println("show int", m)
}

别名定义

定义和原来一样的类型,就是一个alias

type nameMap = map[string]interface{}

func main(){
    m := make(nameMap)
    m["name"] = "golang"
    fmt.Printf("%v", m)
}

type A int32
type B = int32

func main() {
    var a A = 333
    var b B = 333
    b=B(a) // a,b 属于不同的类型,所以这里强制转换
}

定义函数类型

type cb func(s string)  // 定义一个函数类型

//对函数类型再定义方法
func (f cb) ServerCb() error{
    f("cbb")
    fmt.Println("server cb")
    return nil
}

func main() {
    s :=cb(func( s string){
        fmt.Println("sss", s)
    })
    s.ServerCb()
}

有一个关于 type 示例

package main

import (
    "fmt"
)

func main() {
    first(1, callback)
}

func callback(i int) {
    fmt.Println("I am callback")
    fmt.Println(i)
}

func first(i int, f func(int)) {
    Second(i, fun(f))
}

func Second(i int, call Call) {
    call.call(i)
}

type fun func(int)

func (f fun) call(i int)  {
    f(i)
}
type Call interface {
    call(int)
}

参考

PreviousGolang-sync.onceNextGolang-代码生成

Last updated 4 years ago

Was this helpful?

*

type 关键字使用