6

my project organisation looks like this :

  • GOPATH
    • src
      • cvs/user/project
        • main.go
        • utils
          • utils.go

main.go looks like this :

package main import ( "fmt" "cvs/user/project/utils" ) func main() { ... utilsDoSomething() ... } 

and utils.go :

package utils import ( "fmt" ) func utilsDoSomething() { ... } 

The compiler tells me that :

main.go imported and not used: "cvs/user/project/utils" main.go undefined: utilsDoSomething 

I don't know what I'm doing wrong. Any idea would be helpful, thank you in advance !

4 Answers 4

11

You forgot the package prefix in main.go and your function is not exported, meaning it is not accessible from other packages. To export an identifier, use a capital letter at the beginning of the name:

utils.UtilsDoSomething() 

Once you have the utils prefix you can also drop the Utils in the name:

utils.DoSomething() 

If you want to import everything from the utils package into the namespace of your main application do:

import . "cvs/user/project/utils" 

After that you can use DoSomething directly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer !
1

When you are referencing a function from a package you need to reference it using the package name.Function name (with capital case).

In your case use it as:

utils.UtilsDoSomething().

Comments

1

In Go the symbols (including function names) starting with lower case letters are not exported and so are not visible to other packages.

Rename the function to UtilsDoSomething.

If you strongly oppose exporting this function and you are using Go 1.5 or later, you can make your function visible to only your project by placing utils directory inside internal directory.

Specification of internal packages

Comments

0

function name also needs to be capitalized in the package utils

func UtilsDoSomething() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.