当前位置:网站首页>Three uses of golang underscores
Three uses of golang underscores
2022-06-30 14:07:00 【Zhen Jie.】
1. Ignore return value
This should be the simplest use , For example, a function returns three parameters , But we only need two of them , Another parameter can be ignored , So the code can be written like this :
v1, v2, _ := function(...)
2. Used in variables ( In particular, interface assertions )
For example, we define an interface (interface):
type Food interface {
Eat()
}
Then a structure is defined (struct)
type Water struct {
}
Then we want to judge in the code Water This struct Is it implemented Food This interface
var _ Food = Water{
}
The above is used to judge Water Is it implemented Food, Used as a type assertion , If Water It didn't come true Food, Compile error will be reported
3. Use in import package
Suppose we are in the code import In this way package :
import _ "test/food"
This means that... Will be called before executing this code test/food Initialization functions in (init), In this way, only the imported package is initialized , Instead of using other functions in the package
So let's say we define one Food struct, Then initialize it
package food
import "fmt"
type Food struct {
Id int
Name string
}
func init() {
f := &Food{
Id: 123, Name: "apple"}
fmt.Printf("init foo object: %v\n", f)
}
And then in main Introduce... Into the function test/food
package main
import (
"fmt"
_ "test/food"
)
func main() {
fmt.Printf("hello world\n")
}
The operation results are as follows
init food object: &{
123 apple}
hello world
We can see : stay main Output function ”hello world” I've been right before food object It's initialized ( Is loaded test/food Of documents init() Method )!
边栏推荐
- [the path of system analyst] Chapter V software engineering (software process improvement)
- Inexplicable error occurred in unity's frequent switching branch result model
- Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution
- [redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
- 提权扫描工具
- STM32 porting the fish component of RT thread Standard Edition
- Loss function: Diou loss handwriting implementation
- What network security problems are exposed when a large-scale QQ number theft event occurs?
- Assertions of regular series
- Begin End use the pit encountered
猜你喜欢

单元测试效率优化:为什么要对程序进行测试?测试有什么好处?

SQL attendance statistics monthly report

Optimization of unit test efficiency: why test programs? What are the benefits of testing?

【刷题篇】爱吃香蕉的珂珂

我如何才能保护我的私钥?

Deep understanding Net (2) kernel mode 4 Summary of kernel pattern constructs

This editor will open source soon!

Waving flags and shouting for basic language

Lifting scanning tool

Getting started with shell Basics
随机推荐
MySQL如何将列合并?
可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹
【观察】智能产业加速,为何AI算力要先行?
提权扫描工具
(8)JMeter元件详解之 Once only Controller 仅一次控制器
【刷题篇】避免洪水泛滥
Rpm2rpm packaging steps
"Persistent diseases" that cannot be solved in IM application development
Comprehensively analyze the basic features and summary of free and paid SSH tools
Meaning of while (~scanf ("%d%d", & A, & B))
【科研数据处理】[实践]类别变量频数分析图表、数值变量分布图表与正态性检验(包含对数正态)
Service online governance
为基础性语言摇旗呐喊
数字时代,XDR(扩展检测与响应)的无限可能
[scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)
SQL attendance statistics monthly report
How does MySQL merge columns?
Step by step | help you easily submit Google play data security form
SQL programming problem, test case failed
Geoffreyhinton: my 50 years of in-depth study and Research on mental skills