9

Daily development is inseparable from third-party libraries. Most of the time, these libraries meet our needs, but sometimes, we need to fork a copy and make some modifications. As the official package manager of the current go language, go mod naturally takes this situation into consideration. In the go.mod file, use the replace instruction to replace the old library address with the new library address to achieve this operation.

Here is an example to explain the use of go replace and how to deal with common problems.

We first create a new project and quote ozgio/strutil: String utilities for Go (github.com) , and then write a piece of code to ensure that it can work normally:

package main

import (
    "fmt"

    "github.com/ozgio/strutil"
)

func main() {
    fmt.Println(strutil.Align("lorem ipsum", strutil.Right, 20))
}

Initialization of go mod

go mod init project_name

After the go mod init command is executed, the go.mod file will be automatically generated. In this file, the third-party packages that the project depends on and the versions used are listed.

Then execute go mod tidy , this command does two things:

  1. Parse the project file and find the package used
  2. Generate a go.sum file, which saves the version of the package used

Then execute go run main.go to execute the project.

Now, the project should be able to execute normally, and the execution result will be returned.

Suppose we want to call a method for filtering the HTML tags in a string, but there is nothing after flipping it, so we fork a copy of this library, and we add it ourselves:

https://github.com/shiweifu/strutil/blob/master/escape.go

Let's see how to call this new method.

The first way:

  1. Fork the strutil library, open the go.mod file, and modify the module name in the first line to a new name
  2. Increase the required method
  3. Add new git tag
  4. In your current project, quote your modified repo, replace the address and version number

This method is equivalent to referencing a new library, which has nothing to do with the previous library. Most of the time, because of too much code modification, we don't want to use it this way. Of course, go mod also takes this into consideration. Go mod provides the go mod replace method to deal with this scenario.

The second way:

  1. Fork the strutil library
  2. Increase the required method
  3. In the current project, execute the go mod edit -replace command:
go mod edit -replace [old git package]@[version]=[new git package]@[version]

After executing the command, we opened the go.mod file and found that there was an additional command at the bottom:

replace github.com/ozgio/strutil v0.3.0 => github.com/shiweifu/strutil v0.3.0

The go mod replace command supports a specified version number, which can be a git tag or a combination of git commit date + git commit hash.

You can get the latest version of a branch through the following instructions:

go get github.com/shiweifu/strutil@master

At this time, the latest commit record of the master

github.com/shiweifu/strutil@v0.3.1-0.20210615145512-3bd39e22cb0d 

Copy this version number to the go.mod file replace instruction, replace the corresponding version number with this, and then execute it again, you can use our own fork's strutil:

package main

import (
    "fmt"

    "github.com/ozgio/strutil"
)

func main() {
    out := strutil.EscapeHTMLTag("<script>abc</script>")
    fmt.Println(out)
}

We are still github.com/ozgio/strutil the library EscapeHTMLTag is our newly added method. This method is only go.mod , and then we can ozgio/strutil a pr to 060c8ce93aff26. If our code is merged into the warehouse, we can put the replace statement Deleted, this method did not destroy the original code.


shiweifu
2k 声望744 粉丝