Using packages outside Gopath

Hi guys,

I’m probably missing something very simple and basic, but still cannot figure out what.

I have a go-mod folder out of Go path with main.go file and a folder called hello, where I have world.go file.

  • go-mod
    – main.go
    – hello
    — world.go

When I try to do:

func main() {
hello.World()
}

it cannot find “hello”. Importing doesn’t work (in go path import will work and look like import "gitlab.intra/agrechka/go-mod/hello".

go mod init and go mod tidy doesn’t solve the situation.

MacOS, Go v1.11

Thanks!

Hi Artem,

I tried this just now and it worked for me. It worked with GOPATH set to a completely different directory, outside the directory tree that go-mod is in, and with GOPATH not set at all. I’m using Go 1.11 on Linux.

In file main.go:

package main

import "./hello"

func main() {
        hello.World()
}

In file hello/world.go:

package hello

import "fmt"

func World() {
        fmt.Printf("hello, world\n")
}

$ go run main.go
hello, world
$ go build main.go
$ ./main
hello, world

The trick was with import notation, didn’t see or use “./…” before. Many thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.