# Struct Constructor

**URL:** <https://forum.golangbridge.org/t/struct-constructor/11543>\
**Category:** Getting Help\
**Created:** [November 26, 2018, 11:39am UTC](https://forum.golangbridge.org/t/struct-constructor/11543 "2018-11-26T11:39:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![baixeing](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/baixeing/32/4572_2.png) [@baixeing](https://forum.golangbridge.org/u/baixeing)\
**Post date:** [November 26, 2018, 11:39am UTC](https://forum.golangbridge.org/t/struct-constructor/11543/1 "2018-11-26T11:39:11Z")

</div>

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 correct to use this kind of constructors with an extra-parameter (actual “receiver” as far as I understand)?

---

<div class="post-metadata">

**Author:** ![heatbr](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/h/9dc877/32.png) [@heatbr](https://forum.golangbridge.org/u/heatbr)\
**Post date:** [November 26, 2018, 12:24pm UTC](https://forum.golangbridge.org/t/struct-constructor/11543/2 "2018-11-26T12:24:23Z")

</div>

I like to follow the community code examples.

I never saw that kind of code. I usually see :

```auto
func MakeEtc(foo string, bar int) Etc {
...
}

//or something like this
func MakeEtc(foo string, bar int, etc *Etc) {
...
}

```

---

<div class="post-metadata">

**Author:** ![baixeing](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/baixeing/32/4572_2.png) [@baixeing](https://forum.golangbridge.org/u/baixeing)\
**Post date:** [November 26, 2018, 1:23pm UTC](https://forum.golangbridge.org/t/struct-constructor/11543/3 "2018-11-26T13:23:31Z")

</div>

Thanks,

BTW, as I understand **golang** naming rules, `New` functions should return pointer and `Make` functions should return value. Is it right?

---

<div class="post-metadata">

**Author:** ![Yamil\_Bracho](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/yamil_bracho/32/5141_2.png) [@Yamil\_Bracho](https://forum.golangbridge.org/u/Yamil_Bracho)\
**Post date:** [November 26, 2018, 2:42pm UTC](https://forum.golangbridge.org/t/struct-constructor/11543/4 "2018-11-26T14:42:14Z")

</div>

Correct!!

---

<div class="post-metadata">

**Author:** ![derekkenney](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/derekkenney/32/4673_2.png) [@derekkenney](https://forum.golangbridge.org/u/derekkenney)\
**Post date:** [November 28, 2018, 5:10am UTC](https://forum.golangbridge.org/t/struct-constructor/11543/5 "2018-11-28T05:10:20Z")

</div>

Here is an example of how I write a constructor. This is a sample for a microservices app. I prefix my constructors with “New”

```
//Each Aggregate in a microservices application has
//it's own repository.
package main

import "fmt"

type CustomerRepository interface {
    Add()
}

type CustomersRepository struct {
   DatabaseName string
   CollectionName string
}

func main(){
   n := NewCustomersRepository("testDB", "testCollection")
   fmt.Printf("Created new CustomersRepository instance %T\n %s %s \n", n, 
   n.DatabaseName, n.CollectionName)
}

func NewCustomersRepository(database string, collection string) *CustomersRepository {
   return &CustomersRepository {
    DatabaseName: database,
    CollectionName: collection,
   }  
}
```

---

<div class="post-metadata">

**Author:** ![imatmati](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/imatmati/32/4187_2.png) [@imatmati](https://forum.golangbridge.org/u/imatmati)\
**Post date:** [November 28, 2018, 9:44am UTC](https://forum.golangbridge.org/t/struct-constructor/11543/6 "2018-11-28T09:44:06Z")

</div>

Hi,

No, it’s not correct. You create a struct function then you call it by referencing through the struct itself and pass it a Etc object to fulfill the receiver parameter !! Why would you pass a Etc object to generate one without using it ?  
The correct way would be :

```
package main

import (
   "fmt"
)

type Etc struct {
   Foo string
   Bar int
}

func Make(foo string, bar int) Etc {
   return Etc{
  	  Foo: foo,
	  Bar: bar,
   }
}
func main() {
    fmt.Printf("%#+v\n", Make("foo_string", 0xbeef))
}

```

Have a nice day.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [February 26, 2019, 9:44am UTC](https://forum.golangbridge.org/t/struct-constructor/11543/7 "2019-02-26T09:44:11Z")

</div>

Closed on February 26, 2019, 9:44am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
