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 helps keep names short without loosing clarity.

package main

import "fmt"

type Employee struct {

Name string

Age int


func (e *Employee) Init(name string, age int) {

e.Name = name

e.Age = age


func info(name string, age int) *Employee {

e := new(Employee)

e.Name = name

e.Age = age  

return e


func main() {

emp := new(Employee)

emp.Init("John Doe",25)

fmt.Printf("%s: %d\n", emp.Name, emp.Age)

empInfo := info("John Doe",25)



Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Любишь мемасики?

Подпишись на мой телеграм-канал!