匿名结构的意外返回

我正在尝试实现一种方法,该方法返回基于原始结构的修改后的结构,例如:


type Project struct {

    Username string           

    Id       uint      

    Alias    string           

    Data     *json.RawMessage 

    Scheme   Scheme          

}


func (p *Project) OmitUsername() *struct {


    return &struct {

        Id      uint         

        Alias   string   

        Data    *json.RawMessage

        Scheme  Scheme          

    }{

        p.Id,

        p.Alias,

        p.Data,

        p.Scheme

    })

}

我收到以下错误:


models/project.go:22: syntax error: unexpected return 

models/project.go:24: non-declaration statement outside function body 

models/project.go:25: non-declaration statement outside function body 

models/project.go:25: syntax error: unexpected string literal, expecting semicolon or newline 

models/project.go:26: non-declaration statement outside function body

任何帮助,将不胜感激。


MMMHUHU
浏览 155回答 2
2回答

哔哔one

使用“真正的”匿名结构返回值如果您想使用匿名结构体返回值,那看起来会非常难看。为什么?因为在定义返回类型时,必须描述匿名结构体。当你写一个return语句时,你必须提供一个结构体的返回值。匿名结构的结构文字也必须描述结构!当您尝试编写此内容时:func (p *Project) OmitUsername() *struct {&nbsp; &nbsp; // return somethig}这个语法不是你想的那样:它不包含结构定义。基本上在您的示例中,第一个{是匿名结构定义的左括号,而不是函数体的左括号。因此,后续return被解释为在匿名结构定义中,这是无效语法,这也正是错误消息所述的内容 ( "syntax error: unexpected return")。它应该是这样的:func (p *Project) OmitUsername() *struct {&nbsp; &nbsp; Id&nbsp; &nbsp; &nbsp;uint&nbsp; &nbsp; Alias&nbsp; string&nbsp; &nbsp; Data&nbsp; &nbsp;*json.RawMessage&nbsp; &nbsp; Scheme Scheme} {&nbsp; &nbsp; // And now here comes the return statement}如果您还添加了必须重复匿名结构定义的 return 语句:func (p *Project) OmitUsername() *struct {&nbsp; &nbsp; Id&nbsp; &nbsp; &nbsp;uint&nbsp; &nbsp; Alias&nbsp; string&nbsp; &nbsp; Data&nbsp; &nbsp;*json.RawMessage&nbsp; &nbsp; Scheme Scheme} {&nbsp; &nbsp; return &struct {&nbsp; &nbsp; &nbsp; &nbsp; Id&nbsp; &nbsp; &nbsp;uint&nbsp; &nbsp; &nbsp; &nbsp; Alias&nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; Data&nbsp; &nbsp;*json.RawMessage&nbsp; &nbsp; &nbsp; &nbsp; Scheme Scheme&nbsp; &nbsp; }{p.Id, p.Alias, p.Data, p.Scheme}}是的,它很丑。您可以通过使用命名返回值而不是返回指针来简化它,因为指针的零值是nil,并且要返回某些内容,您必须对其进行初始化,这也涉及重复匿名结构!如果您使用非指针命名返回值,您将立即获得匿名结构的值,并且您不必再次重复匿名结构定义,只需为其字段赋值:func (p *Project) OmitUsername2() (ret struct {&nbsp; &nbsp; Id&nbsp; &nbsp; &nbsp;uint&nbsp; &nbsp; Alias&nbsp; string&nbsp; &nbsp; Data&nbsp; &nbsp;*json.RawMessage&nbsp; &nbsp; Scheme Scheme}) {&nbsp; &nbsp; ret.Id = p.Id&nbsp; &nbsp; ret.Alias = p.Alias&nbsp; &nbsp; ret.Data = p.Data&nbsp; &nbsp; ret.Scheme = p.Scheme&nbsp; &nbsp; return}使用它们:p := Project{"Bob", 1, "bobie", nil, nil}fmt.Println(p.OmitUsername())fmt.Println(p.OmitUsername2())输出(在Go Playground上试试这些):&{1 bobie <nil> <nil>}{1 bobie <nil> <nil>}还是丑。。。使用另一种命名类型,使用嵌入...最好是提供另一个命名类型来返回而不是匿名结构。您可以利用嵌入使此解决方案实用且简短:type BaseProject struct {&nbsp; &nbsp; Id&nbsp; &nbsp; &nbsp;uint&nbsp; &nbsp; Alias&nbsp; string&nbsp; &nbsp; Data&nbsp; &nbsp;*json.RawMessage&nbsp; &nbsp; Scheme Scheme}type Project struct {&nbsp; &nbsp; BaseProject&nbsp; &nbsp; Username string}func (p *Project) OmitUsername() BaseProject {&nbsp; &nbsp; return p.BaseProject}使用它:p := Project{BaseProject{1, "bobie", nil, nil}, "Bob"}fmt.Println(p.OmitUsername())输出(在Go Playground上试试这个):{1 bobie <nil> <nil>}笔记:嵌入并不是真正必要的,但是这样嵌入类型 ( BaseProject)的字段将被提升,因此您可以像p.Id在Project. 将其定义为常规字段也可以。

饮歌长啸

以下关键字是保留的,不能用作标识符。break&nbsp; &nbsp; &nbsp; &nbsp; default&nbsp; &nbsp; &nbsp; func&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;interface&nbsp; &nbsp; selectcase&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;defer&nbsp; &nbsp; &nbsp; &nbsp; go&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; structchan&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;package&nbsp; &nbsp; &nbsp; switchconst&nbsp; &nbsp; &nbsp; &nbsp; fallthrough&nbsp; if&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;range&nbsp; &nbsp; &nbsp; &nbsp; typecontinue&nbsp; &nbsp; &nbsp;for&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; import&nbsp; &nbsp; &nbsp; &nbsp;return&nbsp; &nbsp; &nbsp; &nbsp;var.func (p *Project) OmitUsername() *struct {}struct 是保留关键字。如果没有关于您正在尝试做什么的更多信息,就很难知道您想要什么,也许是这样的?package mainimport (&nbsp; &nbsp; "encoding/json")type Scheme struct{}type Project struct {&nbsp; &nbsp; Id&nbsp; &nbsp; &nbsp;uint&nbsp; &nbsp; Alias&nbsp; string&nbsp; &nbsp; Data&nbsp; &nbsp;*json.RawMessage&nbsp; &nbsp; Scheme Scheme}type UserProject struct {&nbsp; &nbsp; Username string&nbsp; &nbsp; Project}func (u *UserProject) GetProject() *Project {&nbsp; &nbsp; return &u.Project}func main() {}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go