L
L
Leksisi2021-02-18 23:57:03
go
Leksisi, 2021-02-18 23:57:03

golang. Self-written package is not visible from the main module?

Started learning Go and just can't beat a simple problem.

Go 1.16

In accordance with the examples, I do this:
1. Set GOPATH = c:\projects\go
2. In the c:\projects\go folder I create the src subfolder
3. In the scr folder I create the mytest directory and folders, I get the following structure:

src\ mytest\package1
src\mytest\cmd

4. In package1 I create a package1.go file:

package paсkage1

// RetVal test function
func RetVal() string {
  return "passed"
}


5. In cmd I create the file main.go
package main

import (
  "fmt"
  "mytest/package1"
)

func main() {
  fmt.Println("Test of package:")
  fmt.Println(package1.RetVal())
}


When I try to execute the code with the go run main.go command, whatever I do, I get an error:

main.go:5:2: package mytest/package1 is not in GOROOT (c:\go\src\mytest\paсkage1)


Why doesn't main.go see this package? In videos on YouTube, everything works for people.
I do 1 in 1 like they do - it does not work.
Copying examples from the Internet - does not work.
I can't figure out what's wrong.

It works only if you copy it to the system folder c:\go\src\mytest\package1
I don't want to clog the system folder, and plus then the question arises of how to add such code to git.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2021-02-19
@Leksisi

Either get an old version of Go like the youtubers have, or finally start using go modules .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question