How to create and use your own packages in Go?
Hi everyone! I just started learning Go, before that I wrote in Python. There I could simply create a file and import it via `import my_file`. In Go, I'm completely confused with packages.\n\nI have a project with this structure:\n\n```\nmyproject/\n├── main.go\n└── math/\n └── helpers.go\n```\n\nIn `helpers.go` I want to write a simple addition function:\n\n```go\npackage math\n\nfunc Add(a int, b int) int {\n return a + b\n}\n```\n\nAnd in `main.go` I'm trying to use it:\n\n```go\npackage main\n\nimport "myproject/math"\n\nfunc main() {\n sum := math.Add(2, 3)\n fmt.Println(sum)\n}\n```\n\nBut the compiler gives an error: `could not import "myproject/math"`. I tried different paths in import, ran `go mod init`, but it still doesn't work. What am I doing wrong? How to properly organize code in Go so that
No answers yet.
Log in to leave an answer