Constructors in Go

前端 未结 11 1490
挽巷
挽巷 2020-12-04 05:24

I have a struct and I would like it to be initialised with some sensible default values.

Typically, the thing to do here is to use a constructor but since go isn\'t

相关标签:
11条回答
  • 2020-12-04 05:59

    Golang is not OOP language in its official documents. All fields of Golang struct has a determined value(not like c/c++), so constructor function is not so necessary as cpp. If you need assign some fields some special values, use factory functions. Golang's community suggest New.. pattern names.

    0 讨论(0)
  • 2020-12-04 06:00

    I am new to go. I have another pattern taken from other languages, that have constructors. And will work in go.

    1. Create an init method.
    2. Make the init method an (object) once routine. It only runs the first time it is called (per object).
    func (d *my_struct) Init (){
        //once
        if !d.is_inited {
            d.is_inited = true
            d.value1 = 7
            d.value2 = 6
        }
    }
    
    1. Call init at the top of every method of this class.

    This pattern is also useful, when you need late initialisation (constructor is too early).

    Advantages: it hides all the complexity in the class, clients don't need to do anything.

    Disadvantages: you must remember to call Init at the top of every method of the class.

    0 讨论(0)
  • 2020-12-04 06:01

    another way is;

    package person
    
    type Person struct {
        Name string
        Old  int
    }
    
    func New(name string, old int) *Person {
        // set only specific field value with field key
        return &Person{
            Name: name,
        }
    }
    
    0 讨论(0)
  • 2020-12-04 06:02

    There are actually two accepted best practices:

    1. Make the zero value of your struct a sensible default. (While this looks strange to most people coming from "traditional" oop it often works and is really convenient).
    2. Provide a function func New() YourTyp or if you have several such types in your package functions func NewYourType1() YourType1 and so on.

    Document if a zero value of your type is usable or not (in which case it has to be set up by one of the New... functions. (For the "traditionalist" oops: Someone who does not read the documentation won't be able to use your types properly, even if he cannot create objects in undefined states.)

    0 讨论(0)
  • 2020-12-04 06:08

    There are some equivalents of constructors for when the zero values can't make sensible default values or for when some parameter is necessary for the struct initialization.

    Supposing you have a struct like this :

    type Thing struct {
        Name  string
        Num   int
    }
    

    then, if the zero values aren't fitting, you would typically construct an instance with a NewThing function returning a pointer :

    func NewThing(someParameter string) *Thing {
        p := new(Thing)
        p.Name = someParameter
        p.Num = 33 // <- a very sensible default value
        return p
    }
    

    When your struct is simple enough, you can use this condensed construct :

    func NewThing(someParameter string) *Thing {
        return &Thing{someParameter, 33}
    }
    

    If you don't want to return a pointer, then a practice is to call the function makeThing instead of NewThing :

    func makeThing(name string) Thing {
        return Thing{name, 33}
    }
    

    Reference : Allocation with new in Effective Go.

    0 讨论(0)
提交回复
热议问题