<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thomas Ersfeld</title>
    <description>The latest articles on DEV Community by Thomas Ersfeld (@tersfeld).</description>
    <link>https://dev.to/tersfeld</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F475236%2F36b145cb-4b23-44a7-aa71-d7419db6b813.jpeg</url>
      <title>DEV Community: Thomas Ersfeld</title>
      <link>https://dev.to/tersfeld</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tersfeld"/>
    <language>en</language>
    <item>
      <title>Private Go modules on a privately hosted github instance</title>
      <dc:creator>Thomas Ersfeld</dc:creator>
      <pubDate>Fri, 25 Sep 2020 15:09:39 +0000</pubDate>
      <link>https://dev.to/tersfeld/private-golang-modules-on-a-private-hosted-github-instance-4jnj</link>
      <guid>https://dev.to/tersfeld/private-golang-modules-on-a-private-hosted-github-instance-4jnj</guid>
      <description>&lt;p&gt;Go modules are highly dependant on github to fetch modules, it is fine in most cases as tons of modules are available publicly on it.&lt;/p&gt;

&lt;p&gt;It can get tricky when you got your private Go libraries that are not hosted on github.com. Here is a simple solution to pull them from your git instance without breaking the fetching of public dependencies on Github.&lt;/p&gt;

&lt;p&gt;This will only work on Go 1.13 with the introduction of GOPRIVATE env.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create your private lib
&lt;/h2&gt;

&lt;p&gt;Create a simple private repo at your instance with that form:&lt;br&gt;
your.hosted.github.instance.com/my_organization/privatelib&lt;/p&gt;

&lt;p&gt;Add one file containing a sample Go file, named for example, lib.go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package privatelib

import "fmt"

func MyPrivateCall() {
  fmt.Println("My super secret lib!")
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Don't forget the capital letter at the start of your exported resources (functions, enums, structs...) to mark them as exported, in this example MyPrivateCall() will be exported.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generate a token in your github instance
&lt;/h2&gt;

&lt;p&gt;You can generate tokens with this direct link : &lt;a href="https://your.hosted.github.instance.com/settings/tokens/new"&gt;https://your.hosted.github.instance.com/settings/tokens/new&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Always get the least privileges needed for your token (can read private repos).&lt;/p&gt;

&lt;h2&gt;
  
  
  Use your private lib in your main project
&lt;/h2&gt;

&lt;p&gt;In your main Go project, you can now import your private lib at the top of the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package api

import (
    "fmt"
    //...

    privatelib "your.hosted.github.instance.com/my_organization/privatelib"
)

// ...


func myApiHandler() {
    privatelib.MyPrivateCall()
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We need to indicate to go mod to inject our credentials when a module hosted on &lt;a href="https://your.hosted.github.instance"&gt;https://your.hosted.github.instance&lt;/a&gt; is parsed in go.mod :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go env -w GOPRIVATE=your.hosted.github.instance
go config --global url."https://your_username:your_token@your.hosted.github.instance.com".insteadOf / "https://your.hosted.github.instance"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Replace your_token by the token generated in step 2.&lt;/p&gt;

&lt;p&gt;Now we fetch and build :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go mod download
go build ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I hope that was useful :)&lt;/p&gt;

</description>
      <category>go</category>
      <category>github</category>
    </item>
  </channel>
</rss>
