结构字面量使用无键字段

huangapple go评论77阅读模式
英文:

struct literal uses unkeyed fields

问题

我的目标是将函数嵌入到现有类型中。

我正在遵循《Effective Go》(https://go.dev/doc/effective_go#arrays)。

问题是它警告说var parent *embedding.Parent github.com/kidfrom/learn-golang/embedding.Child struct literal uses unkeyed fields

目前的解决方案是创建NewChild(parent *Parent) *Child。然而,我担心这只是在愚弄编译器,将来可能会出现意外的错误,那么我做错了什么?

func NewChild(parent *Parent) *Child {
	return &Child{parent}
}

cmd/test/main.go

package main

import "github.com/kidfrom/learn-golang/embedding"

func main() {
	parent := &embedding.Parent{}
	child := &embedding.Child{parent} // 它警告说`var parent *embedding.Parent github.com/kidfrom/learn-golang/embedding.Child struct literal uses unkeyed fields`
	child.CallParentMethod()
}

embedding.go

package embedding

import "fmt"

type Parent struct{}

func (p *Parent) parentMethod() {
	fmt.Println("parent method")
}

type Child struct {
	*Parent
}

func (c *Child) CallParentMethod() {
	c.parentMethod()
}
英文:

My goal is to embed function to an existing type.

I am following Effective Go

The problem is it warns var parent *embedding.Parent
github.com/kidfrom/learn-golang/embedding.Child struct literal uses unkeyed fields
.

The current solution is to create NewChild(parent *Parent) *Child. However, I am afraid that this is just tricking the compiler and in the future it will panic unexpectedly, so what am I doing wrong?

func NewChild(parent *Parent) *Child {
	return &Child{parent}
}

cmd/test/main.go

package main

import "github.com/kidfrom/learn-golang/embedding"

func main() {
	parent := &embedding.Parent{}
	child := &embedding.Child{parent} // it warns `var parent *embedding.Parent
github.com/kidfrom/learn-golang/embedding.Child struct literal uses unkeyed fields`
	child.CallParentMethod()
}

embedding.go

package embedding

import "fmt"

type Parent struct{}

func (p *Parent) parentMethod() {
	fmt.Println("parent method")
}

type Child struct {
	*Parent
}

func (c *Child) CallParentMethod() {
	c.parentMethod()
}

答案1

得分: 5

你收到的警告很可能来自go-staticcheck。运行以下命令也会看到类似的警告:

$ go vet

./main.go:8:12: github.com/kidfrom/learn-golang/embedding.Child composite literal uses unkeyed fields

检查你导入的包的文档:

$ go doc "github.com/kidfrom/learn-golang/embedding" Child

package embedding // import "github.com/kidfrom/learn-golang/embedding"

type Child struct {
        *Parent
}

func NewChild(parent *Parent) *Child
func (c *Child) CallParentMethod()

显示在Child中嵌入的类型是Parent,所以为了修复警告,需要显式地给这个(嵌入的结构体)字段赋值:

child := &embedding.Child{Parent: parent}
英文:

The warning you are getting is most likely from go-staticcheck. You'd also see a similar warning by running:

$ go vet

./main.go:8:12: github.com/kidfrom/learn-golang/embedding.Child composite literal uses unkeyed fields

Checking the docs of the package you are importing:

$ go doc "github.com/kidfrom/learn-golang/embedding" Child

package embedding // import "github.com/kidfrom/learn-golang/embedding"

type Child struct {
        *Parent
}

func NewChild(parent *Parent) *Child
func (c *Child) CallParentMethod()

shows the embedded type within Child is Parent, so to fix the warning explicitly assign the value to this (embedded struct) field:

child := &embedding.Child{Parent: parent}

答案2

得分: 1

你可以通过在Child结构体中的*Parent属性上添加一个键来解决警告。示例代码如下:

type Child struct {
    Parent *Parent
}

然后在main函数中调整child属性的初始化,如下所示:

child := &embedding.Child{Parent: parent}
英文:

You can fix the warning by adding a key to your *Parent attribute within the Child struct. Example:

type Child struct {
    Parent *Parent
}

and adjust the initialization of your child attribute in your main func as followed:

child := &embedding.Child{Parent: parent}

huangapple
  • 本文由 发表于 2022年9月23日 20:32:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/73827790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定