php editor Zimo brings you an article on how to use the workspace method to extend the cloned golang library. In the daily development process, we often use third-party libraries to improve efficiency and functionality. However, sometimes we may need to make some customized modifications to these libraries. This article will introduce a method to extend the cloned golang library using a workspace. With simple steps, you can easily modify and customize any library to meet your specific needs. Whether you are a beginner or an experienced developer, this article can help you better use the golang library. Let’s explore this fun technique together!
I am writing a golang application and want to leverage existing libraries. But the library was missing some features, so I decided to fork the repository and add my new features to it.
To be able to use forked/extended libraries in my application, I used golang's "workspace" feature.
go.work
in my application repository looks like
<code>go 1.21.4 use ( . ../go-git-cmd-wrapper // this is the local path to the forked/edited lib repo ) </code>
This works fine as long as I only extend existing packages in the forked repository. ie. As soon as I start creating a new package in the forked repository, I get a build error like this:
I added a new package containing the following to the forked/cloned repository go-git-cmd-wrapper/log/log.go
<code>package log .... // my new functionality </code>
<code>package main import ( .... // the following three work as expected, i.e. I can add new functionality // to those packages and use them in my application gwbranch "github.com/ldez/go-git-cmd-wrapper/v2/branch" gwconfig "github.com/ldez/go-git-cmd-wrapper/v2/config" gw "github.com/ldez/go-git-cmd-wrapper/v2/git" // the following doesn't work, I get import error on build gwlog "github.com/ldez/go-git-cmd-wrapper/v2/log" .... ) </code>
go: finding module for package github.com/ldez/go-git-cmd-wrapper/v2/log go: changeme imports github.com/ldez/go-git-cmd-wrapper/v2/log: module github.com/ldez/go-git-cmd-wrapper/v2@latest found (v2.6.0), but does not contain package github.com/ldez/go-git-cmd-wrapper/v2/log exit status 1
https://www.php.cn/link/b99f4242922cd10313630b0ecccda1dc
You should use the go module's replace function, which is designed for this use case. Keep the fork in your go workspace, but add the following to your go.mod:
module your-module go 1.21.4 require github.com/ldez/go-git-cmd-wrapper v2.6.0 replace github.com/ldez/go-git-cmd-wrapper v2.6.0 => ../go-git-cmd-wrapper
https://go.dev/doc/modules/managing -dependency#local_directory
The above is the detailed content of How to extend cloned golang library using workspace method. For more information, please follow other related articles on the PHP Chinese website!