How to install third-party packages in Go language?

Golang tutorial to learn how to install third-party packages in Go language?
Submitted by IncludeHelp, on October 02, 2021

To install third-party packages: Download & install the third-party packages from the source repository by using the go get command. The go get command will fetch the packages from the source repository and install them on the GOPATH directory. And then by using the import statement, we can import those packages in our program.

Example:

go get gopkg.in/yaml.v1 

This statement will install "yaml" package.

After installing the "yaml" package, use the import statement in the program for reusing the code, as shown below:

import (        
        " gopkg.in/yaml.v1 " 
)

Consider the below example – Here, we are importing a third-party package "golang-set" from the source repository "github.com/deckarep/" and using it in the program.

package main

import (
	"fmt"
	"github.com/deckarep/golang-set"
)

func main() {
	colors := mapset.NewSet()
	colors.Add("Red")
	colors.Add("Blue")
	colors.Add("Green")

	fmt.Printf("%T, %v\n", colors, colors)
}

Output:

*mapset.threadSafeSet, Set{Red, Blue, Green}

Golang FAQ »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.