Accessing to functions on another file in same package

11,984 views
Skip to first unread message

Joan Miller

unread,
Mar 29, 2010, 3:18:57 AM3/29/10
to golang-nuts
If I've 2 source files to build a same package.

Can be accessed one function from a file to the another one?

// file one.go
-----------------------
package foo

func One() {...}
-----------------------


// file two.go
-----------------------
package foo

func Two() {
/* Access to One()? */
}
-----------------------

chris dollin

unread,
Mar 29, 2010, 3:35:38 AM3/29/10
to Joan Miller, golang-nuts
On 29 March 2010 08:18, Joan Miller <pelo...@gmail.com> wrote:
If I've 2 source files to build a same package.

Can be accessed one function from a file to the another one?

"how to write go code" says:

   Go compiles all the source files in a package at once, so one

  file can refer to constants, variables, types, and functions in

  another file without special arrangement or declarations.

A quick look in the spec doesn't reveal such an explicit statement,
but I could have missed it.

--
Chris "allusive" Dollin

Steven

unread,
Mar 30, 2010, 2:13:22 AM3/30/10
to chris dollin, Joan Miller, golang-nuts
"A package in turn is constructed from one or more source files that together declare constants, types, variables and functions belonging to the package and which are accessible in all files of the same package." 

It doesn't quite say the "without special arrangement or declarations" part, but the language essentially says that, by saying "accessible" without any modification, meaning the names are valid.

Joan Miller

unread,
Mar 30, 2010, 4:18:57 AM3/30/10
to golang-nuts
So, it doesn't works to me.

I've:

// shell.go
------------------------
package shutil

func Foo(...) {
fmt.Println(Bin["qwe"])
}
------------------------


// _path.go
------------------------
package shutil

var Bin: map[string] string {
"qwe": "zxc",
}
------------------------

shell.go:27: undefined: Bin


On 30 mar, 06:13, Steven <steven...@gmail.com> wrote:
> On Mon, Mar 29, 2010 at 3:35 AM, chris dollin <ehog.he...@googlemail.com>wrote:


>
>
>
>
>
> > On 29 March 2010 08:18, Joan Miller <pelok...@gmail.com> wrote:
>
> >> If I've 2 source files to build a same package.
>
> >> Can be accessed one function from a file to the another one?
>
> > "how to write go code" says:
>
> >    Go compiles all the source files in a package at once, so one
>
> >   file can refer to constants, variables, types, and functions in
>
> >   another file without special arrangement or declarations.
> > A quick look in the spec doesn't reveal such an explicit statement,
> > but I could have missed it.
>
> > --
> > Chris "allusive" Dollin
>
> "A package in turn is constructed from one or more source files that
> together declare constants, types, variables and functions belonging to the

> package and *which are accessible in all files of the same package.*"http://golang.org/doc/go_spec.html#Packages

chris dollin

unread,
Mar 30, 2010, 4:23:36 AM3/30/10
to Joan Miller, golang-nuts
On 30 March 2010 09:18, Joan Miller <pelo...@gmail.com> wrote:
So, it doesn't works to me.

I've:

// shell.go
------------------------
package shutil

func Foo(...) {
  fmt.Println(Bin["qwe"])
}
------------------------


// _path.go
------------------------
package shutil

var Bin: map[string] string {
 "qwe": "zxc",
}
------------------------

shell.go:27: undefined: Bin

How did you compile them? You have to compile them together, ie both
filenames on the same 8g command line.
 
--
Chris "allusive" Dollin

Joan Miller

unread,
Mar 30, 2010, 4:31:16 AM3/30/10
to golang-nuts
The failure was on my Makefile, sice it was only one of the source
files.


On 30 mar, 08:23, chris dollin <ehog.he...@googlemail.com> wrote:

Steven

unread,
Mar 30, 2010, 5:38:21 PM3/30/10
to Joan Miller, golang-nuts
On Tuesday, March 30, 2010, Joan Miller <pelo...@gmail.com> wrote:
> var Bin: map[string] string {
>  "qwe": "zxc",
> }
This should be:
var Bin = map[string] string {"qwe": "zxc"}

(replaced colon with equals)

And yes, you need to compile both files at the same time. You would
have gotten a syntax error if you'd compiled the second file.

Maybe you should read the language spec, and other documents? It looks
like you're carrying over assumptions from another language...

Reply all
Reply to author
Forward
0 new messages