Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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). -
@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. -
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. -
@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? -
@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 -
@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. -
@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? -
@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.
Related Rants
-
xorith19HTML: Hate This Meaningless Life CSS: Can't Style Shit JS: Just Shit Java: Just another vicious asshole PH...
-
ObiSwagKenobi6> Receive sudden phone call in the middle of the night > Check caller, unknown number > "Either something ba...
-
Papi23Started learning Go It's very interesting language
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
random
go