#How to import local files?
204 messages · Page 1 of 1 (latest)
what is that?
Introduction to Go Modules, https://roberto.selbach.ca/intro-to-go-modules/ - Skippy
Go Modules Reference, https://go.dev/ref/mod - hhhapz
Using go modules, https://go.dev/blog/using-go-modules - Sgt_Tailor
You need to run go mod init <name your module> and then imports are based off of your module name. They're not relative path imports
go mod init my-project
import "my-project/pkg"
mkdir pkg
echo "package pkg"> pkg/pkg.go
okay, tried doing that, but now I can't use main.go
Using go modules, https://go.dev/blog/using-go-modules - Sgt_Tailor
How to Write Go Code, https://go.dev/doc/code - Etzelia
That bottom link also goes over it
screenshot
what is the package of person?
content of go.mod
package hogwarts
import "fmt"
type Person struct {
name string
age int
}
func newPerson(name string, age int) Person {
return Person{name: name, age: age}
}
func (p Person) greet(greeting string) {
fmt.Printf("%s, %s", greeting, p.name)
}```whole person file
directory name src doesn't really align with package
Otherwise it gets wonky
module hogwarts
go 1.19
```whole go.mod file
rename src to hogwarts
src is not how it works in go
I mean, it can
the folder containing go.mod is the lib root
There are a few things going on here, though.
You're importing hogwarts/person but the package is just hogwarts
import "hogwarts/hogwarts"
Your module name is also Hogwarts
But I'm supposed to import the file?
no
No, ignore files when it comes to go packages
you import the package/folder
meaning, every file in the folder which have the same package definition
every .go file
you cannot import files in golang it's all compiled
you import packages which is a directory, and thus all the files inside the directory
every .go file must have the same package definition when they live in the same package/folder
which is the name of the folder
(otherwise your code maintanance and confusion will be endless)
Module name is like a namespace. There are other notable properties, but foremost that's what it's used for.
Hence they are usually called something like github.com/Etzelia/myapp
A package name roughly matches the dir name. They don't need to, but it greatly simplifies things.
So, if you have a directory/package named hogwarts, each of the files start with package hogwarts
Then, to use that package, you import "github.com/Etzelia/myapp/hogwarts"
but I'm not using git
the git url is just the project name
Git only becomes relevant for publishing
the module name
mkdir myapp
cd myapp
go mod init myapp
echo "package main" > main.go
mkdir hogwarts
echo "package hogwarts" > hogwarts/hogwarts.go
echo 'import "myapp/hogwarts"' >> main.go
code .
open a terminal and execute each line
Alright, a bunch of commands to run doesn't teach why anything works
if go has to do all this things to import a file, I can't imagine other stuff...
does help
go only needs one thing
go mod init myapp
go build .
./myapp
initialize the module , build app and run app
you just sent 10 messages which I don't understand much, to import a file.
those terminal commands are for creating a whole project for you to look at in vs code
and see a complete example which is missing a main function in main.go
but at least you will see how imports work with that example project
you on linux :D?
i dont think commands are helpful without explaining what each of them does and it's purpose
I guess i the end it's easier to read docs as it's more text than I can write on a phone
^ you really should understand how to use go modules, it's pretty much necessary and recommended way
it's mandatory if you want to put your projects in a random folder on your computer.
it's more complicated w/o defining your project as a module, imo, so modules are the way to Go
Already read that, like, 20x
what's your operating system
windows
Here is a full repo with what I typed earlier https://github.com/jolheiser/myapp
it shouldn't matter, butok
in vs code
"myapp" is not a good name though?
ctrl + shift + p
terminal
enter
or just open powershell
where you can execute commands from
like go.exe
Ik how to open a terminal, lol
that's great
you also already able to build your main go file, so all you need to do is to go into your project folder and
go.exe mod init <a name>
that module name is you reference point for importing packages
if you create a folder in the folder where your go.mod lives with the name hogwarts
and probably, you don't even need package here, you can store everything in root if your project is not big
it's a common practice in go
and put a hogwarts.go file in there
that hogwarts.go (file name only needs to end with .go, name doesn't matter, but content matters)
that file must contain a package definition
package hogwarts
func Hello() string {
retur "hello"
}
and I also added some test code
very close comparison to packages in Go is index.js in some node directory. What I'm trying to say is in Go youre trying to group entire implementation into one thing and not use it part by part. "util" is very bad name for package but decent name for a file - because util doesn't cover entire implementation, it's rather part of it
now you need to create a main.go in the folder that also contains go.mod
main.go
package main
import (
"<a name>/hogwarts"
"fmt"
)
func main() {
fmt.Println(hogwarts.Hello())
}
<a name> is the name you used in
go.exe mod init <a name>
and it can also be found in your go.mod file
but then naming makes no sense?
since hogwarts should be the project name
not the package inside it
Side note for @mint hinge: take more time to explain why something works. It is much more valuable for people to understand how something works instead of copy pasting code
Have you looked at the repo I linked?
I'm typing the code on a phone keyboard from the top of my head D: I'm struggling here
it's open
and I said, "myapp" is not a good project name
Between that, the comments in the files, and what I typed earlier, does it make a little more sense?
Pro tip: don’t
myapp was not the focus of the example
Answer questions, don’t provide working examples
don't panic we all been here when coming from file-centric language
follow a single oerson's lead to explain it again, rest should just keep out
it's just 20 ppl writing
Which part confuses you in my example?
Let’s take a step back and let @compact mortar try to explain
si
naming, and it's still giving me errors some times
What errors?
things like this go: to add module requirements and sums: go mod tidy
but they don't seem consistent? dunno
this is worse than pip recursive errors
ngl
That shouldn't happen in the example repo I sent.
I would need to see how your project looks now after any changes.
From what I saw earlier,
- The module name might need to be changed
- The directory name might need to be changed
- After the above we can straighten out imports
I still don't understand why would I need to change the names of the directories
they have names that make sense for their purpose
The package name and dir name should be the same.
There are caveats, but for now let's say it's a rule
The name of the root dir can be whatever, but any sub-packages should match their dir name
but then naming goes to hell...
I'm not sure what that means.
like I said about your project... "myapp" is not a good name
I'm not sure why you're hung up on the example project name.
What would you call your project?
hogwarts?
naming is important?
In a real, not example project, sure
func doesSomething(a, b, c interface{}) ```Like, what is this doing?
It's doing something
yea, that or hogwarts_school
what something...
Again, you're getting hung up on the name I used for an example project.
I am aware naming is important
And in the context of my example repo, myapp is a throwaway name
Because it's just an example
bc it's the example you've shown me?
hogwarts is a fine name for your app
what else name did you expect really for example project?
Now then
go mod init wants something roughly equivalent to a namespace.
I would recommend you give it a URI-ish namespace, but you don't necessarily need to
his app just prints 'banana", so fruit_example or just banana_example?
My app is literally just to show how things work together in Go code.
The specific names are irrelevant.
Anyways
What have you given to go mod init?
(Last time I saw, it was just hogwarts)
hogwarts
So your choices are to either have your files in the root dir, next to main.go
Or to put them in sub-packages (in other directories)
At this stage of a small app, either option is probably fine
and what I did is not working
You want to use a package in a directory, then?
doing this cause it's easier and I probably won't understand the other
still gives me error for any reason
those are defined ```go
package src
import (
"math/rand"
"time"
)
type Dice struct {
size int
}
func newDice(size int) Dice {
return Dice{size: size}
}
func (d Dice) rollDice(times int) []int {
rand.Seed(time.Now().Unix())
results := make([]int, times)
for i := 0; i < times; i++ {
results = append(results, rand.Intn(d.size))
}
return results
}
just add export like js?
As an aside, if you haven't done the tour I would recommend doing that as well
I've already done it
So then to export, those need their first letter capitalized, as mentioned in the tour link above.
there are no errors in that link you sent
Correct
You are trying to use newDice from another package.
It's not exported, so you can't.
If it's in another package, you can only use exported things
okay, everything uppercamel.