#How to import local files?

204 messages · Page 1 of 1 (latest)

knotty thistle
#

Have you got a go.mod file

gusty fjord
#

what is that?

merry anvilBOT
knotty thistle
#

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

gusty fjord
#

what...

#

I'm not using git

#

just local stuff

mint hinge
#

go mod init my-project

#

import "my-project/pkg"

#

mkdir pkg

#

echo "package pkg"> pkg/pkg.go

gusty fjord
#

okay, tried doing that, but now I can't use main.go

mint hinge
#

you can

#

go build .

#

or go run .

merry anvilBOT
compact mortar
#

That bottom link also goes over it

gusty fjord
#

didn't work?

#

I think

compact mortar
#

I need more info than "didn't work"

#

Errors?

mint hinge
#

screenshot

gusty fjord
wide token
#

what is the package of person?

mint hinge
#

content of go.mod

gusty fjord
#
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
compact mortar
#

Well

#

Your dir name and package name should match

wide token
#

directory name src doesn't really align with package

compact mortar
#

Otherwise it gets wonky

gusty fjord
#
module hogwarts

go 1.19

```whole go.mod file
mint hinge
#

rename src to hogwarts

gusty fjord
#

?

#

but then it wouldn't make sense?

#

that's the source directory, the lib.

mint hinge
#

src is not how it works in go

compact mortar
#

I mean, it can

mint hinge
#

the folder containing go.mod is the lib root

compact mortar
#

There are a few things going on here, though.

knotty thistle
#

You're importing hogwarts/person but the package is just hogwarts

mint hinge
#

import "hogwarts/hogwarts"

knotty thistle
#

Your module name is also Hogwarts

gusty fjord
mint hinge
#

no

knotty thistle
#

No, ignore files when it comes to go packages

mint hinge
#

you import the package/folder

#

meaning, every file in the folder which have the same package definition

#

every .go file

real zealot
#

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

mint hinge
#

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)

compact mortar
#

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"

compact mortar
#

Re-read what I said

#

First and foremost it's a namespace

mint hinge
#

the git url is just the project name

compact mortar
#

Git only becomes relevant for publishing

mint hinge
#

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

compact mortar
#

Alright, a bunch of commands to run doesn't teach why anything works

mint hinge
#

and then play around

#

a general example

gusty fjord
#

if go has to do all this things to import a file, I can't imagine other stuff...

mint hinge
#

does help

#

go only needs one thing

#

go mod init myapp
go build .
./myapp

#

initialize the module , build app and run app

gusty fjord
#

you say like it's easy.

#

lol

mint hinge
#

the rest is the creation of folders and file content

#

from a terminal

gusty fjord
#

you just sent 10 messages which I don't understand much, to import a file.

mint hinge
#

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?

real zealot
#

i dont think commands are helpful without explaining what each of them does and it's purpose

mint hinge
#

I guess i the end it's easier to read docs as it's more text than I can write on a phone

real zealot
#

^ you really should understand how to use go modules, it's pretty much necessary and recommended way

mint hinge
#

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

gusty fjord
mint hinge
#

what's your operating system

gusty fjord
#

windows

compact mortar
gusty fjord
#

it shouldn't matter, butok

mint hinge
#

in vs code

gusty fjord
compact mortar
#

Of course it isn't

#

It's an example

mint hinge
#

ctrl + shift + p

#

terminal

#

enter

#

or just open powershell

#

where you can execute commands from

#

like go.exe

gusty fjord
mint hinge
#

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

wide token
#

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

mint hinge
#

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

wide token
#

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

mint hinge
#

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

gusty fjord
#

but then naming makes no sense?

#

since hogwarts should be the project name

#

not the package inside it

limber fjord
#

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

compact mortar
#

Have you looked at the repo I linked?

mint hinge
#

I'm typing the code on a phone keyboard from the top of my head D: I'm struggling here

gusty fjord
#

and I said, "myapp" is not a good project name

compact mortar
#

Between that, the comments in the files, and what I typed earlier, does it make a little more sense?

compact mortar
#

myapp was not the focus of the example

limber fjord
#

Answer questions, don’t provide working examples

gusty fjord
#

just forget it, people

#

I'm just getting more lost

wide token
mint hinge
#

follow a single oerson's lead to explain it again, rest should just keep out

#

it's just 20 ppl writing

compact mortar
#

Which part confuses you in my example?

limber fjord
#

Let’s take a step back and let @compact mortar try to explain

mint hinge
#

si

gusty fjord
compact mortar
#

What errors?

gusty fjord
#

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

compact mortar
#

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,

  1. The module name might need to be changed
  2. The directory name might need to be changed
  3. After the above we can straighten out imports
gusty fjord
#

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

compact mortar
#

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

gusty fjord
#

but then naming goes to hell...

compact mortar
#

I'm not sure what that means.

gusty fjord
#

like I said about your project... "myapp" is not a good name

compact mortar
#

I'm not sure why you're hung up on the example project name.

#

What would you call your project?

#

hogwarts?

gusty fjord
compact mortar
#

In a real, not example project, sure

gusty fjord
#
func doesSomething(a, b, c interface{}) ```Like, what is this doing?
compact mortar
#

It's doing something

gusty fjord
gusty fjord
compact mortar
#

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

gusty fjord
compact mortar
#

hogwarts is a fine name for your app

wide token
#

what else name did you expect really for example project?

compact mortar
#

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

gusty fjord
compact mortar
#

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)

gusty fjord
#

hogwarts

compact mortar
#

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

gusty fjord
#

and what I did is not working

compact mortar
#

You want to use a package in a directory, then?

gusty fjord
#

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

}

compact mortar
#

They aren't exported

gusty fjord
#

just add export like js?

compact mortar
#

As an aside, if you haven't done the tour I would recommend doing that as well

gusty fjord
#

I've already done it

compact mortar
#

So then to export, those need their first letter capitalized, as mentioned in the tour link above.

gusty fjord
#

there are no errors in that link you sent

compact mortar
#

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

gusty fjord
#

what are the naming standards on the language?

#

any pages to that?

compact mortar
gusty fjord
#

okay, everything uppercamel.