Call a function from another package in Go

Go

Go Problem Overview


I have two files main.go which is under package main, and another file with some functions in the package called functions.

My question is: How can I call a function from package main?

File 1: main.go (located in MyProj/main.go)

package main

import "fmt"
import "functions" // I dont have problem creating the reference here

func main(){
    c:= functions.getValue() // <---- this is I want to do
}

File 2: functions.go (located in MyProj/functions/functions.go)

package functions

func getValue() string{
    return "Hello from this another package"
}

Go Solutions


Solution 1 - Go

You import the package by its import path, and reference all its exported symbols (those starting with a capital letter) through the package name, like so:

import "MyProj/functions"

functions.GetValue()

Solution 2 - Go

  • You should prefix your import in main.go with: MyProj, because, the directory the code resides in is a package name by default in Go whether you're calling it main or not. It will be named as MyProj.

  • package main just denotes that this file has an executable command which contains func main(). Then, you can run this code as: go run main.go. See here for more info.

  • You should rename your func getValue() in functions package to func GetValue(), because, only that way the func will be visible to other packages. See here for more info.

File 1: main.go (located in MyProj/main.go)

package main

import (
    "fmt"
    "MyProj/functions"
)

func main(){
    fmt.Println(functions.GetValue())
}

File 2: functions.go (located in MyProj/functions/functions.go)

package functions

// `getValue` should be `GetValue` to be exposed to other packages.
// It should start with a capital letter.
func GetValue() string{
    return "Hello from this another package"
}

Solution 3 - Go

Export function getValue by making 1st character of function name capital, GetValue

Solution 4 - Go

you can write

import(
  functions "./functions" 
)
func main(){
  c:= functions.getValue() <-
}

If you write in gopath write this import functions "MyProj/functions" or if you are working with Docker

Solution 5 - Go

In Go packages, all identifiers will be exported to other packages if the first letter of the identifier name starts with an uppercase letter.

=> change getValue() to GetValue()

Solution 6 - Go

  • you need to create a go.mod file in the root directory of your project: go mod init module_name
  • the name of exposed function should start with capital letter
    import(
       "module_name/functions" 
    )
    func main(){
      functions.SomeFunction()
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCABascourtView Question on Stackoverflow
Solution 1 - GoJimBView Answer on Stackoverflow
Solution 2 - GoInanc GumusView Answer on Stackoverflow
Solution 3 - GonickyView Answer on Stackoverflow
Solution 4 - Googuz zeyveliView Answer on Stackoverflow
Solution 5 - GoThao Nguyen TienView Answer on Stackoverflow
Solution 6 - Goac23View Answer on Stackoverflow