aboutsummaryrefslogtreecommitdiff
path: root/_content/doc/tutorial/create-module.html
blob: 094f2541b017d02b2e6c4b9c34abeaad311a1fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!--{
    "Title": "Tutorial: Create a Go module",
    "Breadcrumb": true
}-->

<p>
  This is the first part of a tutorial that introduces a few fundamental
  features of the Go language. If you're just getting started with Go, be sure
  to take a look at
  <a href="/doc/tutorial/getting-started.html">Tutorial: Get started with Go</a>, which introduces
  the <code>go</code> command, Go modules, and very simple Go code.
</p>

<p>
  In this tutorial you'll create two modules. The first is a library which is
  intended to be imported by other libraries or applications. The second is a
  caller application which will use the first.
</p>

<p>
  This tutorial's sequence includes seven brief topics that each illustrate a
  different part of the language.
</p>

<ol>
  <li>
    Create a module -- Write a small module with functions you can call from
    another module.
  </li>
  <li>
    <a href="/doc/tutorial/call-module-code.html">Call your code from another module</a> --
    Import and use your new module.
  </li>
  <li>
    <a href="/doc/tutorial/handle-errors.html">Return and handle an error</a> -- Add simple
    error handling.
  </li>
  <li>
    <a href="/doc/tutorial/random-greeting.html">Return a random greeting</a> -- Handle data
    in slices (Go's dynamically-sized arrays).
  </li>
  <li>
    <a href="/doc/tutorial/greetings-multiple-people.html"
      >Return greetings for multiple people</a
    >
    -- Store key/value pairs in a map.
  </li>
  <li>
    <a href="/doc/tutorial/add-a-test.html">Add a test</a> -- Use Go's built-in unit testing
    features to test your code.
  </li>
  <li>
    <a href="/doc/tutorial/compile-install.html">Compile and install the application</a> --
    Compile and install your code locally.
  </li>
</ol>

<aside class="Note">
  <strong>Note:</strong> For other tutorials, see
  <a href="/doc/tutorial/index.html">Tutorials</a>.
</aside>

<h2 id="prerequisites">Prerequisites</h2>

<ul>
  <li>
    <strong>Some programming experience.</strong> The code here is pretty
    simple, but it helps to know something about functions, loops, and arrays.
  </li>
  <li>
    <strong>A tool to edit your code.</strong> Any text editor you have will
    work fine. Most text editors have good support for Go. The most popular are
    VSCode (free), GoLand (paid), and Vim (free).
  </li>
  <li>
    <strong>A command terminal.</strong> Go works well using any terminal on
    Linux and Mac, and on PowerShell or cmd in Windows.
  </li>
</ul>

<h2 id="start">Start a module that others can use</h2>

<p>
  Start by creating a Go module. In a
  module, you collect one or more related packages for a discrete and useful set
  of functions. For example, you might create a module with packages that have
  functions for doing financial analysis so that others writing financial
  applications can use your work. For more about developing modules, see
  <a href="/doc/modules/developing">Developing and publishing modules</a>.
</p>

<p>
  Go code is grouped into packages, and packages are grouped into modules. Your
  module specifies dependencies needed to run your code, including the Go
  version and the set of other modules it requires.
</p>

<p>
  As you add or improve functionality in your module, you publish new versions
  of the module. Developers writing code that calls functions in your module can
  import the module's updated packages and test with the new version before
  putting it into production use.
</p>

<ol>
  <li>
    Open a command prompt and <code>cd</code> to your home directory.

    <p>
      On Linux or Mac:
    </p>

    <pre>
cd
</pre
    >

    <p>
      On Windows:
    </p>

    <pre>
cd %HOMEPATH%
</pre
    >
  </li>

  <li>
    Create a <code>greetings</code> directory for your Go module source code.

    <p>
      For example, from your home directory use the following commands:
    </p>

    <pre>
mkdir greetings
cd greetings
</pre
    >
  </li>

  <li>
    Start your module using the
    <a
      href="/ref/mod#go-mod-init"
      ><code>go mod init</code> command</a
    >.

    <p>
      Run the <code>go mod init</code> command, giving it your module path --
      here, use <code>example.com/greetings</code>. If you publish a module,
      this <em>must</em> be a path from which your module can be downloaded by
      Go tools. That would be your code's repository.
    </p>

    <p>
        For more on naming your module with a module path, see
        <a href="/doc/modules/managing-dependencies#naming_module">Managing
        dependencies</a>.
    </p>

    <pre>
$ go mod init example.com/greetings
go: creating new go.mod: module example.com/greetings
</pre
    >

    <p>
      The <code>go mod init</code> command creates a go.mod file to track your
      code's dependencies. So far, the file includes only the name of your
      module and the Go version your code supports. But as you add dependencies,
      the go.mod file will list the versions your code depends on. This keeps
      builds reproducible and gives you direct control over which module
      versions to use.
    </p>
  </li>

  <li>
    In your text editor, create a file in which to write your code and call it
    greetings.go.
  </li>

  <li>
    Paste the following code into your greetings.go file and save the file.

    <pre>
package greetings

import "fmt"

// Hello returns a greeting for the named person.
func Hello(name string) string {
    // Return a greeting that embeds the name in a message.
    message := fmt.Sprintf("Hi, %v. Welcome!", name)
    return message
}
</pre
    >

    <p>
      This is the first code for your module. It returns a greeting to any
      caller that asks for one. You'll write code that calls this function in
      the next step.
    </p>

    <p>
      In this code, you:
    </p>

    <ul>
      <li>
        Declare a <code>greetings</code> package to collect related functions.
      </li>
      <li>
        Implement a <code>Hello</code> function to return the greeting.
        <p>
          This function takes a <code>name</code> parameter whose type is
          <code>string</code>. The function also returns a <code>string</code>.
          In Go, a function whose name starts with a capital letter can be
          called by a function not in the same package. This is known in Go as
          an exported name. For more about exported names, see
          <a href="/tour/basics/3">Exported names</a> in the
          Go tour.
        </p>
        <img src="images/function-syntax.png" width="300px" />
      </li>

      <li>
        Declare a <code>message</code> variable to hold your greeting.
        <p>
          In Go, the <code>:=</code> operator is a shortcut for declaring and
          initializing a variable in one line (Go uses the value on the right to
          determine the variable's type). Taking the long way, you might have
          written this as:
        </p>
        <pre>
var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)
</pre
        >
      </li>

      <li>
        Use the <code>fmt</code> package's <a href="https://pkg.go.dev/fmt/#Sprintf">
        <code>Sprintf</code> function</a> to create a greeting message. The
        first argument is a format string, and <code>Sprintf</code> substitutes
        the <code>name</code> parameter's value for the <code>%v</code> format
        verb. Inserting the value of the <code>name</code> parameter completes
        the greeting text.
      </li>
      <li>Return the formatted greeting text to the caller.</li>
    </ul>
  </li>
</ol>

<p>
  In the next step, you'll call this function from another module.
</p>

<p class="Navigation">
  <a class="Navigation-next" href="/doc/tutorial/call-module-code.html"
    >Call your code from another module &gt;</a
  >
</p>