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.

Answer from Denys Séguret on Stack Overflow
Top answer
1 of 11
331

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.

2 of 11
182

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

🌐
TutorialEdge.net
tutorialedge.net › home › "golang development" › "go constructors tutorial"
Go Constructors Tutorial | TutorialEdge.net
February 18, 2022 - Constructors aren’t something that are built in to the language and what I demonstrate below show the equivalents to constructors in Go, but they are more akin to factory functions. It’s fairly common practice to use this approach when creating new structs that require a more-involved setup than just zero-valuing fields on the struct.
Discussions

Default struct constructors?
So you never know which struct is safe to create dorectly and which require factory func Oh but you do. Safe are exported, unsafe are unexported and require exported factory function in order to create instance. Simple as that. More on reddit.com
🌐 r/golang
15
0
March 16, 2025
Struct Constructor
Hi, I used a constructor Make pattern for struct in that way: package main import ( "fmt" ) type Etc struct { Foo string Bar int } func (Etc) Make(foo string, bar int) Etc { return Etc{ Foo: foo, Bar: bar, } } func main() { fmt.Printf("%#+v\n", Etc.Make(Etc{}, "foo_string", 0xbeef)) } Is that ... More on forum.golangbridge.org
🌐 forum.golangbridge.org
0
0
November 26, 2018
Why you should use dedicated constructors in Go
I think you are approaching some of this from the wrong direction. I totally agree that constructors are useful, but Go gives you the tools to make some types usable as their zero values (sync.Mutex, bytes.Buffer, etc etc etc) and so if you can, you generally should because it means (imo) less cognitive burden for users of your type and readers of that code. Obviously types like servers and clients and such that have dependencies or can fail to be usable need constructors, and that is basically what the existence of a constructor should be a clue for. More on reddit.com
🌐 r/golang
13
0
October 18, 2021
go - Golang struct construction generality - Stack Overflow
I have a struct that two other objects implement. In this case, typeA and B repo's. There's some initialization code, which are represented as ellipses here. The initialization code is completely More on stackoverflow.com
🌐 stackoverflow.com
🌐
DEV Community
dev.to › stefanalfbo › constructors-in-go-1om2
Constructors in Go? - DEV Community
April 29, 2024 - Structures in Go can often be initialized using their zero values. However, there are scenarios where these zero values do not represent meaningful defaults, or when initialization requires specific parameters. In my post, func init(){}, I'm making some parallels with OOP constructors when describing the init function in Go.
🌐
Reddit
reddit.com › r/golang › default struct constructors?
r/golang on Reddit: Default struct constructors?
March 16, 2025 -

I'm wondering why go devs doesn't implement optional default constructors for structs. I.e. right now some structs can be created like this:

myStruct := MyStruct{}

But others require initialization, and must be created with factory functions:

anotherStruct := NewAnotherStruct()

So you never know which struct is safe to create dorectly and which require factory func.

With default constructor you would create all structs the same way, i.e.:

myStruct := MyStruct()

If default constructor is defined it is invoked to initialize the struct, it it is not defined then it is similar to MyStruct{}

🌐
CodeSignal
codesignal.com › learn › courses › clean-coding-with-go-structures › lessons › struct-initialization-in-go-best-practices-for-clean-code
Struct Initialization in Go: Best Practices for Clean Code
In Go, constructor functions (commonly prefixed with New) are essential for initializing structs in a known state, enhancing code maintainability and readability. They encapsulate the logic of object creation, ensuring every struct starts correctly. A well-designed constructor function can ...
🌐
Go by Example
gobyexample.com › structs
Go by Example: Structs
Go’s structs are typed collections of fields. They’re useful for grouping data together to form records · This person struct type has name and age fields
🌐
J7mbo
blog.j7mbo.com › constructors-in-go
Constructors in Go | J7mbo | James Mallison
January 8, 2021 - The only way to access them is with a call to New(), which returns only the exported interface containing the exact same methods the struct has. An additional point here is that the Service cannot exist without it's non-nillable dependency (we're not allowing a pointer to be passed in here). This strategy effectively enforced constructor-like semantics one can see in other languages - a single, tightly controlled entrypoint for intialisation that cannot be bypassed (except via the reflect and unsafe packages, which is beyond the scope of this article and likely wouldn't easily sneak past code review anyway).
Find elsewhere
🌐
GitHub
github.com › moznion › gonstructor
GitHub - moznion/gonstructor: A command-line tool to generate a constructor for the struct. · GitHub
A command-line tool to generate a constructor for the struct. $ go install github.com/moznion/gonstructor/cmd/gonstructor@latest · Also, you can get the pre-built binaries on Releases. ... gonstructor depends on goimports for fixing import paths and formatting code, you need to install it: $ go install golang.org/x/tools/cmd/goimports@latest
Starred by 94 users
Forked by 9 users
Languages   Go 97.2% | Makefile 2.8%
🌐
Medium
medium.com › star-gazers › go-options-pattern-da49185a2526
Go Options Pattern. Flexible Constructors in Go | by Guy J Grigsby | Star Gazers | Medium
March 9, 2021 - When I started writing Go, I had been writing Java for a while and I was very used to overloading constructors. While Go “constructors” are really just functions like any other, I didn’t see a good way to initialize a struct in more than one way because Go doesn’t allow overriding functions.
🌐
Soham Kamani
sohamkamani.com › golang › constructors
Golang Constructors - Design Patterns For Initializing Variables
September 19, 2022 - In this post, we will go through the different types of constructors that we can use in Go, and in what situations you should use them. You can view the code to run all examples mentioned here on the Go playground · Composite literals are the most straight-forward way to initialize an object in Go. Constructing a variable using composite literals looks like this: // creating a new struct instance b := Book{} // creating a pointer to a struct instance bp := &Book{} // creating an empty value nothing := struct{}{}
🌐
Go Forum
forum.golangbridge.org › getting help
Struct Constructor - Getting Help - Go Forum
November 26, 2018 - Hi, I used a constructor Make pattern for struct in that way: package main import ( "fmt" ) type Etc struct { Foo string Bar int } func (Etc) Make(foo string, bar int) Etc { return Etc{ Foo: foo, Bar: bar, …
🌐
Reddit
reddit.com › r/golang › why you should use dedicated constructors in go
r/golang on Reddit: Why you should use dedicated constructors in Go
October 18, 2021 - Constructors in Go: An argument for value receiver constructors - Tit Petric · r/golang • · scene-si · upvote · · comments · Suggest resources for studying distributed systems in go. r/golang • · upvotes · · comments · A series of articles about Clean Architecture in go · r/golang • · upvotes · · comments · Struct constructors, validation, visibility, interfaces for internal implementation - bad idea?
🌐
ZetCode
zetcode.com › golang › struct
Working with Structs in Go
The example creates a new pointer to the User struct with the new keyword. There are no built-in constructors in Go.
🌐
hassansin
hassansin.github.io › golang-constructor-like-function-use-cases
Constructor like functions in Go | hassansin
July 24, 2017 - Only way to work with the type is to use the constructor function and use the hash.Hash32 interface methods for any operation. // Private type and private fields type digest struct { s [4]uint32 // some other private fields } // Return type for New() is an interface type func New() hash.Hash { d := new(digest) d.Reset() return d }
🌐
Delft Stack
delftstack.com › home › howto › go › create constructors in golang
How to Create Constructors in Golang | Delft Stack
February 12, 2024 - For creating instances, factory functions or struct functions are more appropriate in Go, which will be discussed in the following sections. There isn’t a concept of a traditional constructor method associated with a struct like in some other object-oriented languages.
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › golang constructor tutorial
Golang Constructor Tutorial | GoLinuxCloud
October 6, 2022 - This means that Go does not offer constructors natively like Java and Python does. The idiomatic way for setting up a new data structure using constructors is to use proper zero values coupled with factory function.
🌐
Medium
blogs.learningdevops.com › demystifying-go-constructors-a-beginners-guide-c9d4345c7d29
Understanding Constructor Functions in Go | by Rajesh Kumar | Medium
March 17, 2025 - In Go, there are no built-in constructors like in Python or Java. Instead, Go uses what are effectively factory functions that return new instances of types. Here’s an example: package main import "fmt" // User is a simple struct representing ...
🌐
Codecademy
codecademy.com › docs › go › structs
Go | Structs | Codecademy
June 20, 2023 - An alternative method is to use ... a constructor function is defined. This will create a new instance of the struct and will set its default values if none are provided....
🌐
J7mbo
blog.j7mbo.com › bypassing-golangs-lack-of-constructors
Bypassing Golang's lack of constructors | J7mbo | James Mallison
January 8, 2021 - Golang doesn't provide constructors but instead advocates for a package-level factory method with the New convention. If only this were then the singular way of initialising an object; unfortunately this is not always necessarily the case..