Why Go doesn't see my local package?

175 views
Skip to first unread message

fsn7...@gmail.com

unread,
Feb 17, 2017, 3:42:17 AM2/17/17
to golang-nuts
In /etc/profile I have:

export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/work
export GOROOT=/usr/local/go

I have 2 source files: test.go (main) and second.go They located in

/home/u001/work/src/github.com/user/test/
When I'm running go run test.go from test it gives me:
test.go:5:2: open /home/u001/work/src/github.com/user/test/second: no such file or directory

Content of test.go:
package second

import "fmt"

var x int
var y int

func init
() {
    x
= 44
    y
= 100
}

func
ShowXY() {
    fmt
.Printf("X:%d;Y%d.",x,y)
}

Content of second.go:
package second

import "fmt"

var x int
var y int

func init
() {
    x
= 44
    y
= 100
}

func
ShowXY() {
    fmt
.Printf("X:%d;Y%d.",x,y)
}

How to be if two source files are in one folder, let's say they both in ~/. It works when I had second.go in second/ and changed line to ../second
In the book "The Way to Go", listing 4.7 - it is said that in example below package is imported from the same directory.
package main
import (
fmt
“./trans
)
var twoPi = 2 * trans.Pi
func main
() {
fmt
.Printf(“2*Pi = %g\n”, twoPi) // 2*Pi = 6.283185307179586
}





Diego Medina

unread,
Feb 17, 2017, 2:00:41 PM2/17/17
to golang-nuts, fsn7...@gmail.com
Hi,

I think you pasted the same content to test.go as well as second.go

in any case, if you use go run, you need to specify all the files involved:

go run test.go second.go

should do the trick

That being said, unless you are writing small scripts using Go, I would not recommend doing this, instead have a main.go with a package main, then if you have some library code you can put them in the package second and then

go install

and then 

run it as $GOPATH/bin/<binary-name-here>

P.S. you can add $GOPATH/bin to your $PATH and then just call

$<binary-name-here>

Also, don't use relative imports like the one you posted from the "The Way to Go", that is't really what you are supposed to do. Instead, you should import it like

import(
)

even when you are inside your test repository.

And finally, you don't need to set GOROOT any more either, unless you have a custom path to it, etc


Thanks

fsn7...@gmail.com

unread,
Feb 18, 2017, 1:34:38 AM2/18/17
to golang-nuts, fsn7...@gmail.com
Thanks.
 
Just in case - this is what I meant for second.go:
package main


import "./second"


func main
(){
 
 second
.ShowXY();
}
 

Angel Java Lopez

unread,
Feb 18, 2017, 10:04:47 AM2/18/17
to golang-nuts
Hi!

Just curious... About:

" don't use relative imports like the one you posted from the "The Way to Go", that is't really what you are supposed to do. "

Why?

What is the difference with "you are supposed to do"? what is the behavior of using the dot?

In other worlds, like NodeJS, it's very common to have local code consumed in that way. And it is independent of any package directory structure.

Angel "Java" Lopez
@ajlopez


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Harden

unread,
Feb 18, 2017, 4:44:26 PM2/18/17
to Angel Java Lopez, golang-nuts

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Diego Medina

unread,
Feb 18, 2017, 4:46:44 PM2/18/17
to golang-nuts
see inline comments:



" don't use relative imports like the one you posted from the "The Way to Go", that is't really what you are supposed to do. "

Why?

What is the difference with "you are supposed to do"? what is the behavior of using the dot?


There are several reasons you can find on this mailing list and other public forums, many are about tooling support, where some don't support relative path all that well, to me, it's about being explicit about what you depend on. when I import the full path, I can then copy that import and the related code to another package and it will wok, but if I start playing into the relative path import, I have to adjust it to make it point to the correct place.


 
In other worlds, like NodeJS, it's very common to have local code consumed in that way. And it is independent of any package directory structure.



Go is very diff than many other languages, good practices there don't always translate to Go's good practices, this being one of them.

Thanks

Diego

 
Angel "Java" Lopez
@ajlopez


To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages