Skip to content

Commit

Permalink
Site updated: 2024-05-28 10:20:58
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyphen committed May 28, 2024
1 parent bad1f5d commit a097a82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions 2024/05/22/go-vendor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<meta property="og:description" content="珍爱生命,远离 vendor,module 解君愁">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2024-05-22T02:46:36.000Z">
<meta property="article:modified_time" content="2024-05-28T02:00:31.863Z">
<meta property="article:modified_time" content="2024-05-28T02:20:44.240Z">
<meta property="article:author" content="hyphennn">
<meta name="twitter:card" content="summary_large_image">

Expand Down Expand Up @@ -265,7 +265,7 @@
<span class="post-meta mr-2">
<i class="iconfont icon-chart"></i>

3.2k words
3.3k words

</span>

Expand All @@ -276,7 +276,7 @@



27 mins
28 mins

</span>

Expand Down Expand Up @@ -347,7 +347,7 @@ <h3 id="版本一-GOPATH-机制"><a href="#版本一-GOPATH-机制" class="heade
<p>这样,你只需要 import 一下就好了:</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs Go"><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;github.com/hyphennn/proj2/waibiwaibi&quot;</span><br></code></pre></td></tr></table></figure>
<p>这种方式是很直观的,因为你引用项目内其他文件夹时你也是这么做的,当项目中被声明 import “xxx” 时,编译器会去寻找 GOPATH&#x2F;src&#x2F;xxx,如果找到,就会将其作为依赖库加入编译。当然,会优先在 $GOROOT 下找标准库。</p>
<p>项目跑的很不错,现在该添加新的功能了。功能太复杂,人力成本非常有限,不过好消息是我们找到了两个来自互联网的包,帮助我们解决了问题,他们分别是:github.com&#x2F;helper1&#x2F;pkg1 和 golang.org&#x2F;helper2&#x2F;pkg2。怎么把这两个包引入呢?一个很直观的思路就是:向使用我们自己的项目一样使用这两个包,参照一下上面 import 寻找目标的原理,我们能很轻易的给出如下目录:</p>
<p>项目跑的很不错,现在该添加新的功能了。功能太复杂,人力成本非常有限,不过好消息是我们找到了两个来自互联网的包,帮助我们解决了问题,他们分别是:github.com&#x2F;helper1&#x2F;pkg1 和 golang.org&#x2F;helper2&#x2F;pkg2。怎么把这两个包引入呢?一个很直观的思路就是:像使用我们自己的项目一样使用这两个包,参照一下上面 import 寻找目标的原理,我们能很轻易的给出如下目录:</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs text">$GOPATH<br> - bin<br> - src<br> - github.com<br> - hyphennn<br> - proj1 // 下面的层级是你的源代码<br> - proj2<br> - helper1<br> - pkg1<br> - golang.org<br> - helper2<br> -pkg2<br></code></pre></td></tr></table></figure>
<p>同样的,import 语句如下:</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs Go"><span class="hljs-keyword">import</span>(<br> <span class="hljs-string">&quot;github.com/hyphennn/proj2/waibiwaibi&quot;</span><br> <span class="hljs-string">&quot;github.com/helper1/pkg1&quot;</span> <span class="hljs-comment">// 让我们省略掉后面的子目录,毕竟都差不多</span><br> <span class="hljs-string">&quot;golang.org/helper2/pkg2&quot;</span><br>)<br></code></pre></td></tr></table></figure>
Expand Down Expand Up @@ -398,11 +398,14 @@ <h2 id="govendor"><a href="#govendor" class="headerlink" title="govendor"></a>go
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs text">&#123;<br> &quot;checksumSHA1&quot;: &quot;GcaTbmmzSGqTb2X6qnNtmDyew1Q=&quot;,<br> &quot;path&quot;: &quot;github.com/pkg/errors&quot;,<br> &quot;revision&quot;: &quot;a2d6902c6d2a2f194eb3fb474981ab7867c81505&quot;,<br> &quot;revisionTime&quot;: &quot;2016-06-27T22:23:52Z&quot;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>从此实例中就可以大体明白 govendor 的工作原理了,并不算非常复杂,我想大约也是因此 govendor 才广受欢迎。</p>
<p>使用 govendor 后,我们项目的目录便可以简化如下:</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs text">$GOPATH<br> - bin<br> - vendor<br> github.com<br> - vendor.json<br> - src<br> - github.com<br> - hyphennn<br> - proj1 // 下面的层级是你的源代码<br> - 其他代码<br> - vendor<br> - vendor.json<br> - proj2<br> - vendor<br> - vendor.json<br> - vendor<br> - vendor.json<br> - golang.org<br> - helper2<br> -pkg2<br></code></pre></td></tr></table></figure>

<p>govendor 提供如下的命令参数,含义参照一下解释就能大致理清楚:</p>
<figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs livecodeserver">sub-<span class="hljs-keyword">command</span><br> init Create <span class="hljs-keyword">the</span> <span class="hljs-string">&quot;vendor&quot;</span> <span class="hljs-built_in">folder</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> <span class="hljs-string">&quot;vendor.json&quot;</span> <span class="hljs-built_in">file</span>.<br> list List <span class="hljs-keyword">and</span> <span class="hljs-built_in">filter</span> existing dependencies <span class="hljs-keyword">and</span> packages.<br> <span class="hljs-built_in">add</span> Add packages <span class="hljs-built_in">from</span> $GOPATH.<br> update Update packages <span class="hljs-built_in">from</span> $GOPATH.<br> remove Remove packages <span class="hljs-built_in">from</span> <span class="hljs-keyword">the</span> vendor <span class="hljs-built_in">folder</span>.<br> status Lists <span class="hljs-keyword">any</span> packages missing, out-<span class="hljs-keyword">of</span>-<span class="hljs-built_in">date</span>, <span class="hljs-keyword">or</span> modified locally.<br> fetch Add <span class="hljs-built_in">new</span> <span class="hljs-keyword">or</span> update vendor <span class="hljs-built_in">folder</span> packages <span class="hljs-built_in">from</span> remote repository.<br> sync Pull packages <span class="hljs-keyword">into</span> vendor <span class="hljs-built_in">folder</span> <span class="hljs-built_in">from</span> remote repository <span class="hljs-keyword">with</span> revisions<br> <span class="hljs-built_in">from</span> vendor.json <span class="hljs-built_in">file</span>.<br> migrate Move packages <span class="hljs-built_in">from</span> <span class="hljs-keyword">a</span> legacy tool <span class="hljs-built_in">to</span> <span class="hljs-keyword">the</span> vendor <span class="hljs-built_in">folder</span> <span class="hljs-keyword">with</span> metadata.<br> <span class="hljs-built_in">get</span> Like <span class="hljs-string">&quot;go get&quot;</span> but copies dependencies <span class="hljs-keyword">into</span> <span class="hljs-keyword">a</span> <span class="hljs-string">&quot;vendor&quot;</span> <span class="hljs-built_in">folder</span>.<br> license List discovered licenses <span class="hljs-keyword">for</span> <span class="hljs-keyword">the</span> given status <span class="hljs-keyword">or</span> import paths.<br> <span class="hljs-built_in">shell</span> Run <span class="hljs-keyword">a</span> <span class="hljs-string">&quot;shell&quot;</span> <span class="hljs-built_in">to</span> make multiple sub-commands more efficient <span class="hljs-keyword">for</span> large<br> projects.<br><br> go tool commands that are wrapped:<br> `+&lt;status&gt;` package selection may be used <span class="hljs-keyword">with</span> them<br> fmt, build, install, clean, test, vet, generate, tool<br> <br>&lt;status&gt;<br> +<span class="hljs-built_in">local</span> (l) packages <span class="hljs-keyword">in</span> your project<br> +external (e) referenced packages <span class="hljs-keyword">in</span> GOPATH but <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> current project<br> +vendor (v) packages <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> vendor <span class="hljs-built_in">folder</span><br> +std (s) packages <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> standard library<br><br> +excluded (x) external packages explicitly excluded <span class="hljs-built_in">from</span> vendoring<br> +unused (u) packages <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> vendor <span class="hljs-built_in">folder</span>, but unused<br> +missing (m) referenced packages but <span class="hljs-keyword">not</span> found<br><br> +program (p) package is <span class="hljs-keyword">a</span> main package<br><br> +outside +external +missing<br> +all +all packages<br></code></pre></td></tr></table></figure>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs text">&lt;sub-command&gt;<br> init Create the &quot;vendor&quot; folder and the &quot;vendor.json&quot; file.<br> list List and filter existing dependencies and packages.<br> add Add packages from $GOPATH.<br> update Update packages from $GOPATH.<br> remove Remove packages from the vendor folder.<br> status Lists any packages missing, out-of-date, or modified locally.<br> fetch Add new or update vendor folder packages from remote repository.<br> sync Pull packages into vendor folder from remote repository with revisions<br> from vendor.json file.<br> migrate Move packages from a legacy tool to the vendor folder with metadata.<br> get Like &quot;go get&quot; but copies dependencies into a &quot;vendor&quot; folder.<br> license List discovered licenses for the given status or import paths.<br> shell Run a &quot;shell&quot; to make multiple sub-commands more efficient for large<br> projects.<br><br> go tool commands that are wrapped:<br> `+&lt;status&gt;` package selection may be used with them<br> fmt, build, install, clean, test, vet, generate, tool<br> <br>&lt;status&gt;<br> +local (l) packages in your project<br> +external (e) referenced packages in GOPATH but not in current project<br> +vendor (v) packages in the vendor folder<br> +std (s) packages in the standard library<br><br> +excluded (x) external packages explicitly excluded from vendoring<br> +unused (u) packages in the vendor folder, but unused<br> +missing (m) referenced packages but not found<br><br> +program (p) package is a main package<br><br> +outside +external +missing<br> +all +all packages<br></code></pre></td></tr></table></figure>

<p>不过,govendor 由于已经不再维护了,因此其在 M 系列芯片的 Mac 上不可用,而且也不清楚高版本 Go 的兼容性。</p>
<p>由于工作项目需要使用 govendor,因此我复制了一份代码,处理了部分兼容性问题,目前在我本地看起来没啥问题。地址:<a target="_blank" rel="noopener" href="https://github.com/hyphennn/govendor">https://github.com/hyphennn/govendor</a> 。但不清楚有没有未知问题。ps. 之所以不 fork,是因为原项目不再维护了,fork 没有任何意义。</p>
<p>由于工作项目需要使用 govendor,因此我复制了一份代码,处理了部分兼容性问题,目前在我本地(Mac M2)看起来没啥问题。地址:<a target="_blank" rel="noopener" href="https://github.com/hyphennn/govendor">https://github.com/hyphennn/govendor</a> 。但不清楚有没有未知问题。<br>ps. 之所以不 fork,是因为原项目不再维护了,fork 没有任何意义。</p>


</div>
Expand Down
Loading

0 comments on commit a097a82

Please sign in to comment.