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>
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>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>
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 1How to fix this kind of error? What am I doing wrong with my workspace and/or forked repository? WorkaroundJust 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-wrapperSee 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!

The article explains how to use the pprof tool for analyzing Go performance, including enabling profiling, collecting data, and identifying common bottlenecks like CPU and memory issues.Character count: 159

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization

The article discusses Go's reflect package, used for runtime manipulation of code, beneficial for serialization, generic programming, and more. It warns of performance costs like slower execution and higher memory use, advising judicious use and best

The article discusses using table-driven tests in Go, a method that uses a table of test cases to test functions with multiple inputs and outcomes. It highlights benefits like improved readability, reduced duplication, scalability, consistency, and a

The article discusses managing Go module dependencies via go.mod, covering specification, updates, and conflict resolution. It emphasizes best practices like semantic versioning and regular updates.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
