亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

結構類型中的條件/可選字段

結構類型中的條件/可選字段

Go
瀟瀟雨雨 2022-06-01 15:07:40
我有一個Node這樣的結構類型:package tree// Enum for node categorytype Level int32const (    Leaf Level = iota + 1    Branch    Root)type Node struct {    Children []*Node    Parent   *Node    X        float32    Y        float32    Z        float32    Dim      float32    Category Level    LeafPenetration float32 // Needed only if category is "Leaf"    RootPadDim float32      // Needed only if category is "Root"}我有兩個字段Node是可選的,僅根據category字段需要:    leafPenetration float32 // Needed only if category is "Leaf"    rootPadDim float32      // Needed only if category is "Root"目前的Node實施還好嗎?結構類型中此類可選/條件字段的最佳實踐是什么?
查看完整描述

3 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

默認情況下,字段初始化為類型的零值——如果float32是0. 為避免這種情況,通常對可選字段使用指針,例如:


type Node struct {

    Children []*Node

    Parent   *Node

    X        float32

    Y        float32

    Z        float32

    Dim      float32

    Category Level


    // Optional fields

    LeafPenetration *float32  // Needed only if category is "Leaf"

    RootPadDim      *float32  // Needed only if category is "Root"

}

指針字段將默認為nil.


查看完整回答
反對 回復 2022-06-01
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

我最終使用了一種非常簡化的方法:


package tree


// Enum for node category

type Level int32

const (

    Leaf Level = iota + 1

    Branch

    Root

)


type Node struct {

    Category Level

    Parent   *Node

    X        float32

    Y        float32

    Z        float32

    Dim      float32


    RootT  float32 // Root thickness

    RootD  float32 // Root diameter

    RootBR float32 // Root bezel ratio of top-bottom, i.e. top D is larger by this much

    LeafP  float32 // Leaf penetration

}


func NewNode(cat Level, parent *Node, x, y, z, dim float32) *Node {

    n := &Node{

        Category: cat,

        Parent:   parent,

        X:        x,

        Y:        y,

        Z:        z,

        Dim:      dim,

    }

    switch n.Category {

    case Leaf:

        n.LeafP = float32(0.3)

    case Root:

        n.RootT = float32(2)

        n.RootD = float32(30)

        n.RootBR = float32(1.08)

    }

    return n

}


查看完整回答
反對 回復 2022-06-01
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

遵循這個指南是個好主意嗎?


例如具有這種Node類型:


package tree


// Enum for node category

type Level int32

const (

    Leaf Level = iota + 1

    Branch

    Root

)


type Node struct {

    Children []*Node

    Parent   *Node

    X        float32

    Y        float32

    Z        float32

    Dim      float32

    Category Level

    // https://github.com/uber-go/guide/blob/master/style.md#functional-options

    Opts []Option

}

實現這樣的選項:


type options struct {

    penetration float32 // Needed only if category is "Leaf"

    base        float32 // Needed only if category is "Root"

}


type Option interface {

    apply(*options)

}


// penetration option

type penetrationOption float32


func (p penetrationOption) apply(opts *options) {

    opts.penetration = float32(p)

}


func WithPenetrationOption(p float32) Option {

    return penetrationOption(p)

}


// base option

type baseOption float32


func (b baseOption) apply(opts *options) {

    opts.base = float32(b)

}


func WithBaseOption(b float32) Option {

    return baseOption(b)

}

使用上述方法,實現變得過于復雜。但是,它可以在未來進一步擴展。雖然不確定它的價值。


查看完整回答
反對 回復 2022-06-01
  • 3 回答
  • 0 關注
  • 250 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號