Calling Functions from External Packages in Go
In Go, you may encounter the need to access functions from different packages. This article will guide you through the process of calling functions from an external package.
Step 1: Import the Package
To access functions from another package, you must first import the package into your current file. This is done using the import keyword followed by the package's import path. In your example, you have imported the "functions" package in your main.go file:
import "functions"
Step 2: Reference the Function
Once the package is imported, you can reference exported functions from that package using their fully qualified names. In Go, exported symbols (functions, variables, types, etc.) start with a capital letter. So, to call the getValue function from the "functions" package, you would do the following:
functions.GetValue()
Modified Code Example
Based on your sample code, here's the modified main.go file to correctly call the getValue function from the "functions" package:
package main import "fmt" import "MyProj/functions" func main(){ c := functions.GetValue() // Call the GetValue function fmt.Println(c) }
Note: Make sure that the function you are calling is exported by starting its name with a capital letter. Also, ensure that the package path in the import statement matches the actual location of the package.
The above is the detailed content of How to Call Functions from External Packages in Go?. For more information, please follow other related articles on the PHP Chinese website!