How to extend cloned golang library using workspace method

WBOY
Release: 2024-02-10 16:48:08
forward
839 people have browsed it

如何使用工作区方法扩展克隆的 golang 库

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!

Question content

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>
Copy after login

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>
Copy after login

For example in my application I imported existing and new packages of lib

<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>
Copy after login
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
Copy after login

How to fix this kind of error? What am I doing wrong with my workspace and/or forked repository?

Workaround

Just using the workspace functionality won't accomplish what you want to do. This is because the go tool that reads the module path in the main import will call the remote repository, which is the original module. Please refer to:

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
Copy after login
See more information here:

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!