当前位置:网站首页>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 )!
边栏推荐
- Calculates the length of the last word in a string, separated by spaces
- get请求与post提交区别的简易理解
- Unity animator parameter
- Google Earth engine (GEE) -- converts string to number and applies it to time search (ee.date.fromymd)
- Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance
- Waving flags and shouting for basic language
- Assertions of regular series
- “即服务”,企业数字化转型的必然选择
- numpy 创建空数组 data = np.empty(shape=[1, 64,64,3])
- What network security problems are exposed when a large-scale QQ number theft event occurs?
猜你喜欢

visualstudio 和sql

可觀測,才可靠:雲上自動化運維CloudOps系列沙龍 第一彈

Unity animator parameter

MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video

Step by step | help you easily submit Google play data security form

How can I protect my private key?

Geoffreyhinton: my 50 years of in-depth study and Research on mental skills

SQL考勤统计月报表

损失函数:DIOU loss手写实现

Rpm2rpm packaging steps
随机推荐
[the path of system analyst] Chapter V software engineering (software process improvement)
Basic syntax of unity script (1) - common operations of game objects
【观察】智能产业加速,为何AI算力要先行?
幸运哈希竞猜系统开发(源码部署)趣投哈希游戏玩法开发(案例需求)
Exlipse operates on multiple rows at the same time. For example, input the same text in multiple lines and columns at the same time
Unity animator parameter
香港回归20余年,图扑数字孪生港珠澳大桥,超震撼
QQ was stolen? The reason is
Click the TD cell of table to open the dialog pop-up window. After obtaining the value, put the value back into the TD cell
How to execute a query SQL
Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
How does MySQL merge columns?
MySQL access denied, opened as Administrator
【科研数据处理】[实践]类别变量频数分析图表、数值变量分布图表与正态性检验(包含对数正态)
[scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)
Pytorch查看模型参数量和计算量
步骤详解 | 助您轻松提交 Google Play 数据安全表单
数字化转型道阻且长,如何迈好关键的第一步
Wuenda 2022 machine learning special course evaluation is coming!
【系统分析师之路】第五章 复盘软件工程(敏捷开发)