<?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: Houssem</title>
    <description>The latest articles on DEV Community by Houssem (@houssemcharf).</description>
    <link>https://dev.to/houssemcharf</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%2F248969%2F2e2050ba-905f-4d04-9528-ac674f6b2415.jpeg</url>
      <title>DEV Community: Houssem</title>
      <link>https://dev.to/houssemcharf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/houssemcharf"/>
    <language>en</language>
    <item>
      <title>migrating from dep2Go modules</title>
      <dc:creator>Houssem</dc:creator>
      <pubDate>Mon, 21 Oct 2019 18:38:08 +0000</pubDate>
      <link>https://dev.to/houssemcharf/migrating-from-dep2go-modules-8f4</link>
      <guid>https://dev.to/houssemcharf/migrating-from-dep2go-modules-8f4</guid>
      <description>&lt;h1&gt;
  
  
  TL;DR
&lt;/h1&gt;

&lt;p&gt;Go modules was introduced at 1.11 before that people used to package thier dependencies using dep package. in order for us to migrate from the dep package manager to go modules all you have to do is follow those steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;move your code outside of GOPATH&lt;/li&gt;
&lt;li&gt;&lt;code&gt;go mod init [module path]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;go mod tidy&lt;/code&gt; ; this will remove unnecessary imports and add indirect ones.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rm -rf vendor/&lt;/code&gt;; this folder is created by dep package and holds all dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;go build&lt;/code&gt;; this commands ensures that everything is okay.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rm -f Gopkg.lock Gopkg.toml&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git commit -m "migration from dep to go modules"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;before go 1.11, dependencies management was not an easy task to handle. the go community came up with various solution for it and one of the popular was &lt;a href="https://golang.github.io/dep/"&gt;dep&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;similar to many dependencies management tools dep has a file that keeps track of all dependencies which called &lt;code&gt;Gopkg.toml&lt;/code&gt; as well as a file to lock the exact version used which is &lt;code&gt;Gopkg.lock&lt;/code&gt;. the folder &lt;code&gt;vendor&lt;/code&gt; holds the dependency files, executing the command &lt;code&gt;dep ensure&lt;/code&gt; will make all the nessecary checks for the application.&lt;/p&gt;

&lt;p&gt;before go 1.11, your project need to be places in &lt;code&gt;GOPATH&lt;/code&gt; and you had to respect the workplace layout&lt;/p&gt;

&lt;p&gt;&lt;a href="https://golang.org/doc/code.html"&gt;workplace layout&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;after go 1.11 your code can be places anywhere, go can handle directly you dependencies with the introduction of go &lt;a href="https://github.com/golang/go/wiki/Modules"&gt;modules&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Migration
&lt;/h1&gt;

&lt;p&gt;after installing go1.1x start moving your code outside of &lt;code&gt;GOPATH&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(my GOPATH was &lt;code&gt;~/Desktop/go&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;~/Desktop/go $ mv ~/go/src/github.com/houssemcharf/vengine .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;now the project is located in &lt;code&gt;~/Desktop/go/vengine&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;executing go modules&lt;/p&gt;

&lt;p&gt;&lt;code&gt;go mod init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;after executing the command you'll notice that 2 files appeared in your directory.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;go.mod | go.sum&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the first one will contain all the dependencies required for your application to run &lt;br&gt;
it should be structured this way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module vengine

go 1.13

require (

    vengine/config v0.0.0
    vengine/utils v0.0.0
    github.com/davecgh/go-spew v1.1.1
    github.com/hashicorp/vault/api v1.0.4 // indirect
    github.com/nlopes/slack v0.6.0
    github.com/sirupsen/logrus v1.4.2 // indirect
    github.com/spf13/viper v1.4.0 // indirect
    github.com/tidwall/gjson v1.3.2 // indirect

)

replace (
    vengine/config v0.0.0 =&amp;gt; ./config
    vengine/utils v0.0.0 =&amp;gt; ./utils

)

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

&lt;/div&gt;



&lt;p&gt;the second file will contain checksum of different depencies to ensure it integrete as well as the it version. it should look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=

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

&lt;/div&gt;



&lt;p&gt;lets go through the first file the &lt;code&gt;module&lt;/code&gt; refers to your application name&lt;code&gt;go 1.13&lt;/code&gt; is the go version. within the &lt;code&gt;require&lt;/code&gt; we ll find a list of dependencies.&lt;br&gt;
to avoid any duplicated dependencies we can execute &lt;code&gt;go mod tidy&lt;/code&gt; followed by &lt;code&gt;go build&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;** &lt;strong&gt;Optional&lt;/strong&gt; ** &lt;br&gt;
sometimes you need to structure your project in different packages and in my case i had two seperate modules that i needed in my project. to avoid having difficulties during the build specially in this case it is recommended to create different modules within the different packages by executing &lt;code&gt;go mod init&lt;/code&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── main.go
├── config
│   ├── config.go
│   ├── config.json
│   └── go.mod
├── go.mod
├── go.sum
├── LICENSE
├── Makefile
├── README.md
├── utils
   ├── go.mod
   ├── utils.go
   └── utils_test.go

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

&lt;/div&gt;



&lt;p&gt;as we can see &lt;code&gt;go.mod&lt;/code&gt; is placed in different packages&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;config&lt;/li&gt;
&lt;li&gt;utils&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;notice that in the previous &lt;code&gt;go.mod&lt;/code&gt; we used replace which point to our freshly created &lt;code&gt;go.mod&lt;/code&gt; within the different packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;replace (
    vengine/config v0.0.0 =&amp;gt; ./config
    vengine/utils v0.0.0 =&amp;gt; ./utils
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it's also worth to mention that those packages are also mentioned in the go.mod within the required directory.&lt;br&gt;
check the example above.&lt;br&gt;
for a final check execute &lt;code&gt;go build&lt;/code&gt;.&lt;/p&gt;

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