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
>
>
> On Fri, Feb 17, 2017 at 4:00 PM, Diego Medina <fmpw...@gmail.com 
> <javascript:>> wrote:
>
>> 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(
>> "github.com/user/test/second"
>> )
>>
>> 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
>>
>>
>>
>>
>>
>> On Friday, February 17, 2017 at 3:42:17 AM UTC-5, fsn7...@gmail.com 
>> wrote:
>>>
>>> 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
>>> }
>>>
>>>
>>>
>>>
>>>
>>> -- 
>> 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 <javascript:>.
>> 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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to