Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Remove a dependency from a module in Go (Golang)

Posted on November 6, 2023November 6, 2023 by admin

Table of Contents

  • Overview
  • Example

Overview

To remove a module dependency we need to do below two things

  • Remove reference of that dependency from the source files of the module
  • Run go mod tidy command. Remove all dependencies from the go.mod file which are not required in the source files.

Example

Let’s say we have a module with import that as”learn” having below go.mod file and learn.go file.

go.mod

module learn
go 1.14
require github.com/pborman/uuid v1.2.1

learn.go

package main
import (
"fmt"
"strings"
"github.com/pborman/uuid"
)
func main() {
uuidWithHyphen := uuid.NewRandom()
uuid := strings.Replace(uuidWithHyphen.String(), "-", "", -1)
fmt.Println(uuid)
}

Notice that we have imported the dependency in the learn.go as well as this dependency is added in the go.mod file

"github.com/pborman/uuid"

Now let’s see try to remove this dependency completely from above module. Command go mod tidy will remove the dependency from go.mod file if it is not required in the source files. To illustrate this let’s remove the learn.go file that we created above. Now run the command

go mod tidy -v

It will give below output

unused github.com/pborman/uuid

Now examine the contents of go.mod file.  It will be as below

module learn
go 1.14

The

require github.com/pborman/uuid v1.2.1

line will be removed as it is not required in any of the source files. Also all entries of github.com/pborman/uuid and its depedencies will be removed from the go.sum file as well.

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP