<?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: prassee</title>
    <description>The latest articles on DEV Community by prassee (@prassee).</description>
    <link>https://dev.to/prassee</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%2F111547%2Fbe91b3f9-1a02-415b-8a11-12295d56243a.jpeg</url>
      <title>DEV Community: prassee</title>
      <link>https://dev.to/prassee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prassee"/>
    <language>en</language>
    <item>
      <title>Intro to Modules on Go - Part 2</title>
      <dc:creator>prassee</dc:creator>
      <pubDate>Sun, 23 Jun 2019 06:12:37 +0000</pubDate>
      <link>https://dev.to/prassee/intro-to-modules-on-go-part-2-ijj</link>
      <guid>https://dev.to/prassee/intro-to-modules-on-go-part-2-ijj</guid>
      <description>&lt;p&gt;In the previous &lt;a href="https://dev.to/prassee/intro-to-modules-on-go-part-1-1k77"&gt;post&lt;/a&gt; we saw how to init modules, write one, publish it (locally with &lt;code&gt;replace&lt;/code&gt; directive) &amp;amp; use it on another Go project / module. This part we are going to publish the module to github and get rid of &lt;code&gt;replace&lt;/code&gt; way of aliasing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Publishing to Github&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Publshing to github is a no-brainer just init a git repo and push it to github. However the name of the repo does matters here since the consumers of our module would use that as the identifier.&lt;/p&gt;

&lt;p&gt;Lets change the go.mod file on &lt;code&gt;localmod1&lt;/code&gt;. So that it points to a proper git repo than just a name.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module github.com/prassee/localmod1

go 1.12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now lets push it to github.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init . 
git remote add origin git@github.com:prassee/localmod1.git
...
git cm 'initial commit' 
git push origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now the module is published to &lt;a href="https://github.com/prassee/localmod1"&gt;github&lt;/a&gt;. Lets proceed further to use it on other modules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using in other modules&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This step does not require any coding but we just need to edit &lt;code&gt;go.mod&lt;/code&gt; file. Lets open &lt;code&gt;usingmod1&lt;/code&gt; module's&lt;br&gt;
go.mod file. &lt;/p&gt;

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

replace github.com/prassee/localmod1 =&amp;gt; ../localmod1

go 1.12

require github.com/prassee/localmod1 v0.0.0-00010101000000-00000000
0000 // indirect
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;in the above we just need to replace the 2nd line with the following and updated version should look like below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module usingmod1
require github.com/prassee/localmod1 v1.0.2
go 1.12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now if we run &lt;code&gt;go mod tidy&lt;/code&gt;. we can see its downloading the artifact with the version as we desired.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; go mod tidy
go: finding github.com/prassee/localmod1 v1.0.2
go: downloading github.com/prassee/localmod1 v1.0.2
go: extracting github.com/prassee/localmod1 v1.0.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Notes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Across this excercise we never used &lt;code&gt;go get&lt;/code&gt; command or had any vendor folder to 
manage our dependenceis. &lt;/li&gt;
&lt;li&gt;Modules were written outside the &lt;code&gt;GOPATH&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thats all folks .... I think I had covered the extent I know about Go Modules. &lt;/p&gt;

</description>
      <category>go</category>
      <category>modules</category>
      <category>code</category>
      <category>emacs</category>
    </item>
    <item>
      <title>Intro to Modules on Go - Part 1</title>
      <dc:creator>prassee</dc:creator>
      <pubDate>Sat, 22 Jun 2019 15:21:14 +0000</pubDate>
      <link>https://dev.to/prassee/intro-to-modules-on-go-part-1-1k77</link>
      <guid>https://dev.to/prassee/intro-to-modules-on-go-part-1-1k77</guid>
      <description>&lt;h2&gt;
  
  
  Intro to Modules on Go - Part 1
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What are Go modules?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Starting from Go 1.11 'modules' have been introduced to ease / formalize code sharing between Go projects much like other mainstream languages. &lt;/p&gt;

&lt;p&gt;Go Modules are very handy way to contain the code into  distributable units. &lt;br&gt;
We declare a project as module by using the &lt;code&gt;go mod&lt;/code&gt; command. This modules &lt;br&gt;
created can be included as "dependency". &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Writing a Go Module&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets write a module which doubles a number. A Go module is nothing but a simple Go project but that resides outside your &lt;code&gt;$GOPATH&lt;/code&gt;. Go modules are initialized in project &lt;br&gt;
with the &lt;code&gt;go mod&lt;/code&gt; command. Let create a dir called &lt;code&gt;localmod1&lt;/code&gt; outside $GOPATH &lt;br&gt;
and init module with the command &lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir  localmod1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd localmod1&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;This above step creates a go.mod file. It has the below contents. &lt;/p&gt;

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

go 1.12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create a file called localmod1.go now and put the below code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package localmod1

import (
"fmt"
)

func DblX(x int) int {
fmt.Printf("dboubling %v", x)
return x * 2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now our module is ready with some real code. Next step is to consume it so that we prepare it for other modules to use them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using published module in another Go module&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Modules can be consumed in two ways either from local or directly from github. Though the latter is the most recommended however, on some cases it make sense (and useful) to treat as if they are published locally. &lt;/p&gt;

&lt;p&gt;Lets create another module say &lt;code&gt;usingmod1&lt;/code&gt;  which uses &lt;code&gt;localmod1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir usingmod1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd usingmod1&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;like before the above step creates a empty &lt;code&gt;go.mod&lt;/code&gt; file as shown below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module usingmod1
go 1.12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;local&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now lets use the 'localmod1' by adding it as a dependency. Since localmod1 is still in our local but we can assume that its been published to github thereby creating an alias pointing to location of localmod1 absolute path.  Add the following lines to go.mod &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    require github.com/prassee/localmod1 v0.0.0
    replace github.com/prassee/localmod1 =&amp;gt; ../localmod1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;here the git repo &lt;code&gt;github.com/prassee/localmod1&lt;/code&gt; is an imaginary or yet to create location. After adding lets run &lt;code&gt;go mod tidy&lt;/code&gt; command to let go to formalize our dependency definition. The upadted version  look like below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    module usingmod1
    replace github.com/prassee/localmod1 =&amp;gt; ../localmod1
    go 1.12
    require github.com/prassee/localmod1 v0.0.0-00010101000000-000000000000 // indirect
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now we are all set to use the imported module in our code. In &lt;code&gt;main.go&lt;/code&gt; add the following lines.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package main

    import (
        "fmt"
        "github.com/prassee/localmod1"
    )
    func main() {
        fmt.Printf("using mod1")
        localmod1.DblX(23)
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Thats for now. In the next &lt;a href="https://dev.to/prassee/intro-to-modules-on-go-part-2-ijj"&gt;part&lt;/a&gt; we can explore how to publish a module in to Github .&lt;/p&gt;

&lt;p&gt;until then enjoy Go modules ... &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Iam still picking up Go here are the references I've used to learn myself writing Go modules.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://roberto.selbach.ca/intro-to-go-modules/"&gt;https://roberto.selbach.ca/intro-to-go-modules/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>go</category>
      <category>modules</category>
      <category>code</category>
      <category>emacs</category>
    </item>
  </channel>
</rss>
