Go language pack structure

I'm trying to learn Go and follow existing conventions, but like every agreement, you need to understand them first before using them well, and after some research, I have not found an exact answer to my next question

I created a project inside mine $GOPATH, following a similar structure like this:

$GOPATH/
  github.com/
    username/
      projectname/
        main.go
        numbers/
          rational.go
          real.go
          complex.go

My main thing:

package main

import(
"fmt"
"./numbers"
)

func main() {
    fmt.Println(numbers.Real{2.0})
}

So the questions are:

  • I read that I need to have a file package.goinside each package folder, right?

  • If so, inside numbers.go, how will I import rational.go, real.goand complex.go?

  • And then, is it possible to have something like:

    // real.go
    package numbers
    
    type Real struct {
        Number float64
    }
    

... and basically do fmt.Println(numbers.Real{2.0})?

+3
source share
1 answer

-: src: `$ GOPATH/ src/github.com/..."

: . . , import "github.com/username/projectname/number"

:

  • . Go , , .

  • rational.go, complex.go real.go package numbers. , . . : .

+10

All Articles