Is there a way to avoid writing submodules names

If I’ve the below:

|--main.go
|--models
     |-- defenitions.go

And in definistions.go I’ve the below:

package models
type Person struct {
     name  string
     age   uint
}

If I want to call Person at main.go I’ve to use this;

package main
import "models"

func main(){
     p := models.Person{name: "Karam", age: 5}
}

Is there a way to avoid mentioning models.Person{...} and be able to use only Person{...}

You can type cast models.Person in your main.go, as in:

type Person models.Person

func main {
     p := Person{name: "Karam", age: 5}
     ...
}

If you’re referring to using models.Person directly without the package names prepend to it and without type casting, then it is strictly no-no.

The rule is that the codes must be clear enough to self-explain its defined code sources.

1 Like

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