Quantcast
Channel: Constructors in Go - Stack Overflow
Browsing all 12 articles
Browse latest View live

Answer by user3121260 for Constructors in Go

Many statements say "there's no default constructor in golang", but we can have this code, that compiles and runs. I think this is what a go novice would call default constructorspackage mainimport...

View Article



Answer by ctrl-alt-delor for Constructors in Go

I am new to go. I have a pattern taken from other languages, that have constructors. And will work in go.Create an init method.Make the init method an (object) once routine. It only runs the first time...

View Article

Answer by Liubomyr Mykhalchyshyn for Constructors in Go

In Go, a constructor can be implemented using a function that returns a pointer to a modified structure.type Colors struct { R byte G byte B byte}// Constructorfunc NewColors (r, g, b byte) *Colors {...

View Article

Answer by guillaume blaquiere for Constructors in Go

If you want to force the factory function usage, name your struct (your class) with the first character in lowercase. Then, it won't be possible to instantiate directly the struct, the factory method...

View Article

Answer by Ivan Aracki for Constructors in Go

I like the explanation from this blog post:The function New is a Go convention for packages that create a core type or different types for use by the application developer. Look at how New is defined...

View Article


Answer by Kerem for Constructors in Go

another way is;package persontype 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, }}

View Article

Answer by Sebastian Bartos for Constructors in Go

There are no default constructors in Go, but you can declare methods for any type. You could make it a habit to declare a method called "Init". Not sure if how this relates to best practices, but it...

View Article

Answer by hurricane1026 for Constructors in Go

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...

View Article


Answer by zzzz for Constructors in Go

Go has objects. Objects can have constructors (although not automatic constructors). And finally, Go is an OOP language (data types have methods attached, but admittedly there are endless definitions...

View Article


Answer by Volker for Constructors in Go

There are actually two accepted best practices: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...

View Article

Answer by Denys Séguret for Constructors in Go

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...

View Article

Constructors in Go

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 really OOP in the traditional sense...

View Article
Browsing all 12 articles
Browse latest View live




Latest Images