R
R
Roman Sergeevich2021-03-29 20:14:55
go
Roman Sergeevich, 2021-03-29 20:14:55

How to split a GO project into separate files, and then include them in main?

I don't want to write the path to my local packages through the github repository.
There is a project broken into files in different folders.
How to include them in main?
On import swears.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-03-29
@roman24hs

You can do it through `go mod`, something like this:
1. Create a folder yourproject.com/ wherever you like
2. cd yourproject.com/ ; go mod init yourproject.com
3. Create the pkg1 package inside yourproject.com, i.e. yourproject.com/pkg1
4. Create main.go
5. Run the command go run .
Project structure

yourproject.com/main.go
yourproject.com/go.mod (появится после выполнения команды go mod init)
yourproject.com/pkg1/pkg1.go

yourproject.com/main.go
package main

import "yourproject.com/pkg1"

func main() {
    pkg1.SomeFunc()
}

yourproject.com/pkg1/pkg1.go
package pkg1

import "fmt"

func SomeFunc() {
    fmt.Println(`SomeFunc called`)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question