5
gitpush
4y

I finally got the time for learning Go.
Anyways I'm reading this about packages, and made me wonder, what exactly a package is in terms of Go lang?

That example extracts two functions into a new package, and that made me confused

Comments
  • 2
    A package is an importable library or an executable (if the package is main).
    In source-code, a package is just a directory with >=1 go-files, that all share the same package name, usually either main or the name of the directory.
    If the the package name is main, the package compiles into an executable, else, it compiles into a static library (.a).
  • 1
    @metamourge Thanks man, can we have a package name as: x.y.z?
  • 1
    @gitpush
    No,
    You can't do relative imports, as all packages are linked independent.
    If you have a package "x", that contains a package " y", that contains a package "z", you would put

    import(
    " x/y/z"
    )

    In the import-section of the file and use z to refer to it.
  • 1
    Packages in go... are a bit complicated.
    1. There is no centerlized repo for packges (like npm, or pip, or gem, etc). you import from a public git repo. example: import "github.com/random/randomly".
    2. You can name an import like js:
    import (
    myRandom "github.com/random/randomly"
    )
    3. You cannot put two packages in the same dir.
    4. Exported methods start with a capital letter, privates lowercase.
    5. you can do some things in your go.mod file, and override package imports with others.
  • 0
    @metamourge What I understood is I can have multiple packages in a project, say like Java? The only difference in java we can do x.y.z whereas in Go its only x

    And in Go each package takes the name of its parent folder? So I can something like what you see in the image?
  • 0
    @magicMirror Thank you for help :D
    If I may ask, can you give an example of #5? The go.mod I have only contains, module name and go version.

    One last question, which is best for a rest service:
    using something like this:

    func main() {
    initEvents()
    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/", homeLink)
    router.HandleFunc("/event", createEvent).Methods("POST")
    router.HandleFunc("/events", getAllEvents).Methods("GET")
    router.HandleFunc("/events/{id}", getOneEvent).Methods("GET")
    router.HandleFunc("/events/{id}", updateEvent).Methods("PATCH")
    router.HandleFunc("/events/{id}", deleteEvent).Methods("DELETE")
    log.Fatal(http.ListenAndServe(":8080", router))
    }

    Or something like the attached image
  • 1
    @gitpush
    Yes, that's about right.
  • 0
    @metamourge Thanks man much appreciated :)
  • 1
    @gitpushusing the replace directive in a go.mod, allows you to do two things:
    1. use a modified lib, for development, then remove replace for production.
    2. solve a tricky depedency import issues. hopefully - you will not need it.

    and the x.y.z thing - it is similar to the python import statement:
    from blip.blop import blipblop as blipblipblop
    so you can use blipblipblop.
  • 0
    @magicMirror Thanks man much appreciated, I'm still trying to figure this out since I mostly work with C# for backend services.

    So far I managed a simple API, so far that's what I managed to do, ignore the fact I have zero error handling in that code, but all in all, is this how to write Go?
  • 0
    @gitpush I don't really understand your code.
    I'm pretty sure you can marshal your slice of structs directly to json, without the for loop though.
Add Comment