Import package (third party) from GitHub
Description This guide assumes that you already have created your own package in go and uploaded on GitHub, if you haven't then checkout create a package in go and upload package to GitHub. For demonstration we will use package uploaded here.
Explanation The package, in the repository here (GoModule.go) uploaded on GitHub, consist of a function name hello which takes string argument and returns a formatted message along with string.
// package hosted on GitHub
package greetings
import "fmt"
// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name)
return message
}
Below is our main package (In Go, application code that must be executed should be main package), where we must import and use function from the third party package (package other than standard library are called third party, in this case it is our package hosted on GitHub)
package main
import (
"fmt"
// we import the package here
"github.com/shariqkhan29/GoModule"
)
func main() {
// Get a greeting message and print it.
message := greetings.Hello("Gladys")
fmt.Println(message)
}
Now, type go run . in your command prompt you should see output as below
Last updated
Was this helpful?