directory name versus package name

311 views
Skip to first unread message

Dorival Pedroso

unread,
Mar 29, 2017, 3:18:52 AM3/29/17
to golang-nuts
Hello,

I haven't noticed that the code below (located at $GOPATH/src/MyWrapper):

package mylib

import "fmt"

func SayHello() {
fmt.Println("hello")
}

is actually being installed (go install) as a MyWrapper package, even though the package name was explicitly given as mylib.

I couldn't find in the documentation where the handling of this situation is described; i.e. how the final package name is decided (apparently the directory name is chosen).

Anyway, so, why using package mylib then?

Cheers.
Dorival


Jan Mercl

unread,
Mar 29, 2017, 5:18:08 AM3/29/17
to Dorival Pedroso, golang-nuts
On Wed, Mar 29, 2017 at 9:19 AM Dorival Pedroso <dorival...@gmail.com> wrote:

> Anyway, so, why using package mylib then?

'mylib' is the implicit qualifier used to access the exported identifiers from that package from within a package importing it.

--

-j

Scott Lewis-Kelly

unread,
Mar 29, 2017, 8:32:50 AM3/29/17
to golang-nuts
This is touched upon in this blog post. It is confusing that the import path would not match the package it contains. 

Dorival Pedroso

unread,
Mar 29, 2017, 7:41:02 PM3/29/17
to golang-nuts
I like the idea that the go tool would enforce matching directory and package names.

This would help us in case we forget to be consistent...

Jan Mercl

unread,
Mar 30, 2017, 3:11:20 AM3/30/17
to Dorival Pedroso, golang-nuts
On Thu, Mar 30, 2017 at 1:41 AM Dorival Pedroso <dorival...@gmail.com> wrote:

> I like the idea that the go tool would enforce matching directory and package names.

I don't know why it should be enforced, but consider that commands must be in package main.

--

-j

Reply all
Reply to author
Forward
0 new messages