首页 > 其他分享 >Go - Using Multiple Versions of the Same Dependent Packages

Go - Using Multiple Versions of the Same Dependent Packages

时间:2023-09-29 09:33:19浏览次数:53  
标签:github Multiple Versions Dependent package mux gorilla go com

Problem: You want to use multiple versions of the same dependent packages in your code.


Solution: Use the replace directive in the go.mod file to rename your package.

Though it might seem like a very niche requirement, there is sometimes a need to be able to use multiple versions of the same package in your project. In the previous recipe, we talked about one possible use — if you want to migrate a large codebase incrementally but still enable it to work while you’re migrating, you might want to use different versions of the same package.
Another possible reason is when you have a dependent package that works only with a specific version of another package. This is more common than you might think because packages developed by different developers move according to their project schedules. You could be in trouble if one of your critical dependent packages still uses an older version of a shared dependent package.
Semantic import versions work well because the major versions require a change in the path. However, this wouldn’t work in the case of minor version upgrades. In this case, you can use the replace directive to rename a package and use the new name to import it into your code.


First, change the go.mod file to rename your packages. Say you’re using both v1.8.0 and v1.7.4 for gorilla/mux .
You rename these packages accordingly using the replace directive:

go 1.20

replace github.com/gorilla/mux/180 => github.com/gorilla/mux v1.8.0
replace github.com/gorilla/mux/174 => github.com/gorilla/mux v1.7.4

Run go get to get the package versions using their new names:

$ go get github.com/gorilla/mux/174
$ go get github.com/gorilla/mux/180

If you open your go.mod , you should see the two new packages being required:

go 1.20

replace github.com/gorilla/mux/180 => github.com/gorilla/mux v1.8.0
replace github.com/gorilla/mux/174 => github.com/gorilla/mux v1.7.4

require (
    github.com/gorilla/mux/174 v0.0.0 - 00010101000000 - 000000000000 // indirect
    github.com/gorilla/mux/180 v0.0.0 - 00010101000000 - 000000000000 // indirect
)

Now you can go back to your code and import these two packages:

package   main 

import   ( 
      "log" 
      "net/http" 
      "text/template" 

      mux174   "github.com/gorilla/mux/174" 
      mux180   "github.com/gorilla/mux/180" 
) 

func   main ()   { 
      r   :=   mux180 . NewRouter () 
      r . HandleFunc ( "/{text}" ,   name ) 
      log . Fatal ( http . ListenAndServe ( ":8080" ,   r )) 
} 

func   name ( w   http . ResponseWriter ,   r   * http . Request )   { 
      vars   :=   mux174 . Vars ( r ) 
      t ,   _   :=   template . ParseFiles ( "static/text.html" ) 
      t . Execute ( w ,   vars [ "text" ]) 
}

Note that while this program builds and runs because you are using two separate packages, the mux174.Vars will not be able to get the path from the URL.
What happens if you vendor the packages now?
Run go mod vendor from the command line. Now open your vendor package. You should be able to see two different versions of the package under two different directories as if they are different packages altogether.

 

标签:github,Multiple,Versions,Dependent,package,mux,gorilla,go,com
From: https://www.cnblogs.com/zhangzhihui/p/17736785.html

相关文章

  • Go - Requiring Local Versions of Dependent Packages
    Problem: Youwanttouselocalversionsofthedependentpackages.Solution: SetupGotouseavendordirectorybyrunninggomodvendor.Localversionsarethespecificversionofthedependentpackagesthatyoucanuseandareasafeguardincasethe......
  • [891] Combine multiple dictionaries in Python
    TocombinemultipledictionariesinPython,youcanuseanyofthemethodsmentionedearlierforcombiningtwodictionaries.Youcanrepeatedlyapplythesemethodstomergemultipledictionariesintoone.Here'showyoucandoit:Usingtheupdate()......
  • ERROR: Could not find a version that satisfies the requirement selunium (from ve
    错误信息ERROR:Couldnotfindaversionthatsatisfiestherequirementselenium(fromversions:none)ERROR:Nomatchingdistributionfoundforselenium解决方案方法1:增大超时时间pip--default-timeout=100installselenium方法2:修改安装源为清华安装源pipi......
  • PIM-DM:Protocol Independent Multicast-Dense Mode
    建立连接的过程1、hello报文(30s)2、扩展3、prune修剪4、断言机制5、嫁接6、刷新(105s)......
  • AtCoder Grand Contest 023 E Inversions
    洛谷传送门AtCoder传送门首先将\(a\)从小到大排序,设\(p_i\)为排序后的\(a_i\)位于原序列第\(p_i\)个位置,\(x_i\)为要填的排列的第\(i\)个数。设\(A=\prod\limits_{i=1}^n(a_i-i+1)\),则\(A\)为排列的总方案数(考虑按\(a_i\)从小到大填即得)。套路地,统......
  • Road To Reality(Multiple valuedness, natural logarithms)
    RoadToReality(Multiplevaluedness,naturallogarithms)Addition-to-multiplication\(e^{a+b}=e^ae^b\)theinverseoftheexponentialfunction:\(z=\ln{w}\)if\(w=e^z\)Hence:\(\ln{ab}=\ln{a}+\ln{b}\)AspecialCartesianform(\(z=x+iy\)......
  • 复合索引 multiple-column index Composite Indexes
    MySQL::MySQL8.0ReferenceManual::8.3.6Multiple-ColumnIndexeshttps://dev.mysql.com/doc/refman/8.0/en/multiple-column-indexes.html MySQL::MySQL8.0ReferenceManual::8.2.1.3IndexMergeOptimizationhttps://dev.mysql.com/doc/refman/8.0/en/inde......
  • 解决Found multiple records While trying to resolve alternate key.
    这是我的第503篇原创文章,写于2023年9月7日。碰到一个奇怪的问题,用户在界面上更新记录不报错,但是通过代码使用WebAPI来更新代码会报错,使用的是普通的更新代码:varclientUrl=Xrm.Utility.getGlobalContext().getClientUrl();varreq=newXMLHttpRequest();varrequestData={......
  • Qt编译报错:multiple definition of
    解决方法一:在.pro文件下查看SOURCES+=和HEADERS+=也没有重复引入文件,删除重复的文件即可。解决方法二:把debug文件夹下的.o文件都删除,然后再编译下。解决方法三:如果.h文件中有类的定义和实现,则实现时在每个成员前加inline。  转载于:QT项目出现multipledefinitionof错误......
  • [React Typescript] Strongly type Shared props for multiple components (React.FC<
    import{Equal,Expect}from"../helpers/type-utils";typeInputProps=React.ComponentProps<"input">;constCOMPONENTS={text:(props)=>{return<input{...props}type="text"/>;},number:(p......