Table of Contents
The meaning of empty interface" >The meaning of empty interface
Empty interface" >Empty interface
空接口在函数中的应用" >空接口在函数中的应用
类型断言" >类型断言
Home Backend Development Golang An article to help you understand the basic interface of Go language (Part 2)

An article to help you understand the basic interface of Go language (Part 2)

Jul 20, 2023 pm 05:24 PM
go language

The meaning of empty interface

This article introduces Storage class interface , storage interface is my name. In fact, others call it empty interface.

The function of the empty interface is to store any data.

Similarly, the empty interface does not define any methods, that is to say any Interfaces all implement empty interfaces, so empty interfaces can save any data.


Empty interface

##Empty interface sample code

package main

import "fmt"

func main() {
    //变量直接赋值为空接口
    var x interface{}
    x = 1
    x = 1.1
    x = "666"
    x = [3]int{1, 2, 3} //数组
    x = []int{6, 23}    //切片
    x = map[string]string{
        "Name": "张三",
        "Age":  "666",
}
    fmt.Println(x)
    //map中value类型为空接口
    var y = map[string]interface{}{
        "Name":"张三",
        "Age":18,
}
    fmt.Println(y)
}
Copy after login

Execution results

An article to help you understand the basic interface of Go language (Part 2)

##The empty interface can indeed store any data and can be used directly.


空接口在函数中的应用

不知道你有没有想过,如果函数的参数,也是一个空接口,那不是就可以随便传值了吗?

我还用个毛静态类型,以后都用空接口得了,嗯。。。这个按下不表,等会说。


函数参数为空接口代码

package main
import "fmt"

func say(x interface{}) {
    fmt.Println(x)
}
func main() {
    say(1)
    say("666")
    say([]int{5, 1, 3})
}
Copy after login

确实是如上述所说,函数参数为空接口类类型,的确可以传任何数据。

但是仅限打印时可以随便打印。

假设我想让传入的值+1

func say(x interface{}) {
    fmt.Println(x)
    fmt.Println(x+1)
}
Copy after login

报错信息

An article to help you understand the basic interface of Go language (Part 2)

所以说,空接口并不是万能的。


类型断言

上述函数虽然可以接收任意类型。

但是我有个需求。

如果传入的是int类型,参数+100。

如果传入的是字符串类型,参数+"666"。

如果传入的是其他类型,返回不存在。

这该怎么办呢?这就要用到断言这个功能。

断言语法

空接口.(类型)
Copy after login

示例代码

func say(x interface{}) {
    value, ok := x.(int)
    if ok {
        //如果是ok表示断言成功,是int类型
        fmt.Println(value + 100)
}
    value2, ok := x.(string)
    if ok {
        //断言成功表示是字符串
        fmt.Println(value2 + "666")
  }
}
Copy after login

如果说有很多类型,断言起来比较麻烦,这时候用switch比较好。

func say(x interface{}) {
    switch value := x.(type) {
    case int:
        fmt.Println(value + 10)
    case string:
        fmt.Println(value + "666")
    default:
        fmt.Println("啥都不是")
  }
}
//case可以继续写,可以写很多很多
Copy after login

执行结果

An article to help you understand the basic interface of Go language (Part 2)


总结

到此为止,Go语言的接口是全部都讲完了。

接口主要分为行为接口存储接口

  • 行为接口主要用在面向对象这个方向,规定父类必须有哪些动作。

  • 空接口时为了方便存值,可以存储任意类型。

The above is the detailed content of An article to help you understand the basic interface of Go language (Part 2). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Why is it necessary to pass pointers when using Go and viper libraries? Why is it necessary to pass pointers when using Go and viper libraries? Apr 02, 2025 pm 04:00 PM

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...

See all articles