diff options
| author | Steve Traut <straut@google.com> | 2021-03-01 10:42:43 -0500 |
|---|---|---|
| committer | Steve Traut <straut@google.com> | 2021-03-02 21:51:57 +0000 |
| commit | 02ef8fa5ddb8111aa8752ffb8267e67c4e209649 (patch) | |
| tree | 13085d9e3c6b067ff0aa923aabcd93c5dc7975b3 /_content/doc/tutorial | |
| parent | 65c83740113547629052d38de46b57b708ec6e2b (diff) | |
| download | go-x-website-02ef8fa5ddb8111aa8752ffb8267e67c4e209649.tar.xz | |
_content/doc: fix module and tutorial bugs and clean up flow
For golang/go#44241
- Fix issues 2, 3, 8, 16, 17, 18 from golang/go#44241
Other changes in multiple topics:
- In markdown, replace HTML anchor tags with {#anchor} tags.
- In a few places, add content to clarify that module path must
be a location from which the module can be downloaded.
- Where it was missing, add example.com domain to example module
paths. Hopefully, this will reinforce the idea that the module
path should typically include a domain. Docs will use
something that looks like a domain name for module path.
- Add more cross-references from tutorial to references for
packages and commands.
- Rewrite a few links so that they include the topic title,
rather than simply inline text. Left those links whose
destinations are references -- the item's name seems to
suggest that a reference is at the destination.
- Remove domain name from golang.org doc links, leaving root
directory. Such as /cmd/go/* or /doc/modules/*
- Add path up to root for all links in the same domain.
Some were linking by file name only.
- Change standard library links from golang.org to pkg.go.dev.
Changes in the module tutorial:
- Add text to help clarify that there should be a hello and
greetings directory as siblings in their directory hierarchy.
Some users thought one should be subordinate to the other.
- Where needed, reorder steps so that `go mod init` is run before
code is added. This is intended to reinforce the importance of
the module's presence.
- In require/replace steps, have the user use `go mod edit`
rather than editing the go.mod file in an editor. The tools
are more likely to yield a functioning result.
- Where possible/appropriate, change module directive link
destinations from "Modules reference" to go.mod reference.
- Change "run the code" steps so that they all use `go run .`
rather than `go build` or `go run <filename>`. This removes
the impedance of explanation and more commands, while
moving the explanation of `go build` and `go install` to
a separate topic where they share a clearer context.
- Add a "Conclusion" topic with a few links. The tutorial ended
rather abruptly before.
- Minor edits to remove some redundant language.
Change-Id: I93055035d73c362ba73edea458fc53bc45e66512
Reviewed-on: https://go-review.googlesource.com/c/website/+/297531
Trust: Steve Traut <straut@google.com>
Run-TryBot: Steve Traut <straut@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to '_content/doc/tutorial')
| -rw-r--r-- | _content/doc/tutorial/add-a-test.html | 42 | ||||
| -rw-r--r-- | _content/doc/tutorial/call-module-code.html | 224 | ||||
| -rw-r--r-- | _content/doc/tutorial/compile-install.html | 124 | ||||
| -rw-r--r-- | _content/doc/tutorial/create-module.html | 81 | ||||
| -rw-r--r-- | _content/doc/tutorial/getting-started.html | 48 | ||||
| -rw-r--r-- | _content/doc/tutorial/greetings-multiple-people.html | 71 | ||||
| -rw-r--r-- | _content/doc/tutorial/handle-errors.html | 29 | ||||
| -rw-r--r-- | _content/doc/tutorial/index.html | 7 | ||||
| -rw-r--r-- | _content/doc/tutorial/module-conclusion.html | 31 | ||||
| -rw-r--r-- | _content/doc/tutorial/random-greeting.html | 101 |
10 files changed, 433 insertions, 325 deletions
diff --git a/_content/doc/tutorial/add-a-test.html b/_content/doc/tutorial/add-a-test.html index d52f3d3b..dffd4924 100644 --- a/_content/doc/tutorial/add-a-test.html +++ b/_content/doc/tutorial/add-a-test.html @@ -12,7 +12,7 @@ <aside class="Note"> <strong>Note:</strong> This topic is part of a multi-part tutorial that begins - with <a href="create-module.html">Create a Go module</a>. + with <a href="/doc/tutorial/create-module.html">Create a Go module</a>. </aside> <p> @@ -73,16 +73,13 @@ func TestHelloEmpty(t *testing.T) { Implement test functions in the same package as the code you're testing. </li> <li> - Create two test functions to test the - <code>greetings.Hello</code> function. Test function names have the form - <code>Test<em>Name</em></code - >, where <em>Name</em> is specific to the test. Also, test functions - take a pointer to the - <a href="https://golang.org/pkg/testing/" - ><code>testing</code> package's</a - > - <code>testing.T</code> as a parameter. You use this parameter's methods - for reporting and logging from your test. + Create two test functions to test the <code>greetings.Hello</code> + function. Test function names have the form <code>Test<em>Name</em></code>, + where <em>Name</em> says something about the specific test. Also, test + functions take a pointer to the <code>testing</code> package's + <a href="https://pkg.go.dev/testing/#T"><code>testing.T</code> + type</a> as a parameter. You use this parameter's methods for reporting + and logging from your test. </li> <li> Implement two tests: @@ -94,18 +91,16 @@ func TestHelloEmpty(t *testing.T) { able to return a valid response message. If the call returns an error or an unexpected response message (one that doesn't include the name you passed in), you use the <code>t</code> parameter's - <code>Fatalf</code> method to print a message to the console and end - execution. + <a href="https://pkg.go.dev/testing/#T.Fatalf"> + <code>Fatalf</code> method</a> to print a message to the console + and end execution. </li> <li> <code>TestHelloEmpty</code> calls the <code>Hello</code> function with an empty string. This test is designed to confirm that your error handling works. If the call returns a non-empty string or no - error, you use the <code>t</code> parameter's - <a href="https://golang.org/pkg/testing/#T.Fatalf" - ><code>Fatalf</code> method</a - > - to print a message to the console and end execution. + error, you use the <code>t</code> parameter's <code>Fatalf</code> + method to print a message to the console and end execution. </li> </ul> </li> @@ -114,7 +109,7 @@ func TestHelloEmpty(t *testing.T) { <li> At the command line in the greetings directory, run the - <a href="https://golang.org/cmd/go/#hdr-Test_packages" + <a href="/cmd/go/#hdr-Test_packages" ><code>go test</code> command</a > to execute the test. @@ -202,16 +197,15 @@ FAIL example.com/greetings 0.182s </ol> <p> - This topic introduced Go's built-in support for unit testing. In the - tutorial's <a href="compile-install.html">next topic</a>, you'll see how to - compile and install your code to run it locally. + In the next (and last) topic, you'll see how to compile and install your code + to run it locally. </p> <p class="Navigation"> - <a class="Navigation-prev" href="greetings-multiple-people.html" + <a class="Navigation-prev" href="/doc/tutorial/greetings-multiple-people.html" >< Return greetings for multiple people</a > - <a class="Navigation-next" href="compile-install.html" + <a class="Navigation-next" href="/doc/tutorial/compile-install.html" >Compile and install the application ></a > </p> diff --git a/_content/doc/tutorial/call-module-code.html b/_content/doc/tutorial/call-module-code.html index 3c3602b8..bce43d72 100644 --- a/_content/doc/tutorial/call-module-code.html +++ b/_content/doc/tutorial/call-module-code.html @@ -4,7 +4,7 @@ }--> <p> - In the <a href="create-module.html">previous section</a>, you created a + In the <a href="/doc/tutorial/create-module.html">previous section</a>, you created a <code>greetings</code> module. In this section, you'll write code to make calls to the <code>Hello</code> function in the module you just wrote. You'll write code you can execute as an application, and which calls code in the @@ -13,7 +13,7 @@ <aside class="Note"> <strong>Note:</strong> This topic is part of a multi-part tutorial that begins - with <a href="create-module.html">Create a Go module</a>. + with <a href="/doc/tutorial/create-module.html">Create a Go module</a>. </aside> <ol> @@ -22,8 +22,16 @@ is where you'll write your caller. <p> - For example, if your current directory in the command prompt is the - greetings directory, you could use the following commands: + After you create this directory, you should have both a hello and a + greetings directory at the same level in the hierarchy, like so: + </p> + <pre><home>/ + |-- greetings/ + |-- hello/</pre> + + <p> + For example, if your command prompt is in the greetings directory, you + could use the following commands: </p> <pre> @@ -35,7 +43,28 @@ cd hello </li> <li> - In your text editor (in the hello directory), create a file in which to + Enable dependency tracking for the code you're about to write. + + <p> + To enable dependency tracking for your code, run the + <a + href="/ref/mod#go-mod-init" + ><code>go mod init</code> command</a>, giving it the name of the module + your code will be in.</p> + + <p> + For the purposes of this tutorial, use <code>example.com/hello</code> + for the module path. + </p> + + <pre> +$ go mod init example.com/hello +go: creating new go.mod: module example.com/hello +</pre> + </li> + + <li> + In your text editor, in the hello directory, create a file in which to write your code and call it hello.go. </li> @@ -61,8 +90,7 @@ func main() { message := greetings.Hello("Gladys") fmt.Println(message) } -</pre - > +</pre> <p> In this code, you: @@ -71,16 +99,16 @@ func main() { <ul> <li> Declare a <code>main</code> package. In Go, code executed as an - application must go in a <code>main</code> package. + application must be in a <code>main</code> package. </li> <li> Import two packages: <code>example.com/greetings</code> and - <code>fmt</code>. This gives your code access to functions in those - packages. Importing <code>example.com/greetings</code> (the package - contained in the module you created earlier) gives you access to the - <code>Hello</code> function. You also import <code>fmt</code>, with - functions for handling input and output text (such as printing text to - the console). + the <a href="https://pkg.go.dev/fmt/"><code>fmt</code> package</a>. This + gives your code access to functions in those packages. Importing + <code>example.com/greetings</code> (the package contained in the module + you created earlier) gives you access to the <code>Hello</code> + function. You also import <code>fmt</code>, with functions for handling + input and output text (such as printing text to the console). </li> <li> Get a greeting by calling the <code>greetings</code> package’s @@ -90,148 +118,124 @@ func main() { </li> <li> - Create a new module for this hello package. + Edit the <code>example.com/hello</code> module to use your local + <code>example.com/greetings</code> module. <p> - From the command line at the hello directory, run the - <code>go mod init</code> command, giving it the name of the module your - code will be in (here, just use "hello"). - </p> - - <pre> -$ go mod init hello -go: creating new go.mod: module hello -</pre - > - </li> - - <li> - Edit the <code>hello</code> module to use the unpublished greetings module. - - <p> - For production use, you’d publish your modules on a server, either inside - your company or on the internet, and the Go command will download them - from there. For now, you need to adapt the caller's module so it can find - the greetings code on your local file system. + For production use, you’d publish the <code>example.com/greetings</code> + module from its repository (with a module path that reflected its published + location), where Go tools could find it to download it. + For now, because you haven't published the module yet, you need to adapt + the <code>example.com/hello</code> module so it can find the + <code>example.com/greetings</code> code on your local file system. </p> <p> - To do that, make a small change to <code>hello</code> module’s go.mod - file. + To do that, use the + <a href="/ref/mod#go-mod-edit"><code>go + mod edit</code> command</a> to edit the <code>example.com/hello</code> + module to redirect Go tools from its module path (where the module isn't) + to the local directory (where it is). </p> <ol> <li> - In the hello directory, open the go.mod file, change it so that it looks - like the following, and save the file. + From the command prompt in the hello directory, run the following + command: + + <pre> +$ go mod edit -replace=example.com/greetings=../greetings +</pre> + + <p> + The command specifies that <code>example.com/greetings</code> should be + replaced with <code>../greetings</code> for the purpose of locating the + dependency. After you run the command, the go.mod file in the hello + directory should include a <a href="/doc/modules/gomod-ref#replace"> + <code>replace</code> directive</a>: + </p> <pre> -module hello +module example.com/hello -go 1.14 +go 1.16 <ins>replace example.com/greetings => ../greetings</ins> </pre> - - <p> - Here, the - <a href="/ref/mod#go-mod-file-replace"> - <code>replace</code> directive - </a> - tells Go to replace the module path (the URL - <code>example.com/greetings</code>) with a path you specify. In this - case, that's a greetings directory next to the hello directory. - </p> </li> <li> - In the hello directory, run <code>go build</code> to make Go locate the - module and add it as a dependency to the go.mod file. + From the command prompt in the hello directory, run the + <a href="/ref/mod#go-mod-tidy"> + <code>go mod tidy</code> command</a> to synchronize the + <code>example.com/hello</code> module's dependencies, adding those + required by the code, but not yet tracked in the module. - <pre> -$ go build + <pre>$ go mod tidy go: found example.com/greetings in example.com/greetings v0.0.0-00010101000000-000000000000 -</pre - > - </li> - - <li> - Look at go.mod again to see the changes made by <code>go build</code>, - including the <code>require</code> directive Go added. +</pre> + <p> + After the command completes, the <code>example.com/hello</code> + module's go.mod file should look like this: + </p> - <pre> -module hello + <pre>module example.com/hello -go 1.14 +go 1.16 replace example.com/greetings => ../greetings -<ins>require example.com/greetings v0.0.0-00010101000000-000000000000</ins> -</pre> +<ins>require example.com/greetings v0.0.0-00010101000000-000000000000</ins></pre> <p> - To build the module, Go found the local code in the ../greetings - directory, then added a - <a href="/ref/mod#go-mod-file-require"> - <code>require</code> directive - </a> - to specify that <code>hello</code> is dependent on (requires) - <code>example.com/greetings</code>. You created this dependency when - you imported the <code>greetings</code> package (contained in the - greetings module) in hello.go. The <code>replace</code> directive - tells Go where to find the <code>greetings</code> module, because it - isn't published yet. + The command found the local code in the greetings directory, then + added a <a href="/doc/modules/gomod-ref#require"><code>require</code> + directive</a> to specify that <code>example.com/hello</code> + requires <code>example.com/greetings</code>. You created this + dependency when you imported the <code>greetings</code> package in + hello.go. + </p> + <p> + The number following the module path is a <em>pseudo-version number</em> + -- a generated number used in place of a semantic version number (which + the module doesn't have yet). </p> - <p> - To reference a published module, a go.mod file would omit the - <code>replace</code> directive and use a - <code>require</code> directive with a tagged version number at the - end. + To reference a <em>published</em> module, a go.mod file would + typically omit the <code>replace</code> directive and use a + <code>require</code> directive with a tagged version number at the end. </p> <pre>require example.com/greetings v1.1.0</pre> + + <p>For more on version numbers, see + <a href="/doc/modules/version-numbers">Module version numbering</a>.</p> </li> </ol> - </li> - <li> - In the <code>hello</code> directory, run the <code>hello</code> executable - (created by <code>go build</code>) to confirm that the code works. - <ul> - <li> - On Linux or Mac: + At the command prompt in the <code>hello</code> directory, run your code to + confirm that it works. - <pre> -$ ./hello -Hi, Gladys. Welcome! -</pre - > - </li> - - <li> - On Windows: - - <pre> -$ hello.exe + <pre> +$ go run . Hi, Gladys. Welcome! -</pre - > - </li> - </ul> +</pre> </li> </ol> <p> - Congrats! You've written two functioning modules. In the tutorial's - <a href="handle-errors.html">next topic</a>, you'll add some error handling. + Congrats! You've written two functioning modules. +</p> + +<p> + In the next topic, you'll add some error handling. </p> <p class="Navigation"> - <a class="Navigation-prev" href="create-module.html" + <a class="Navigation-prev" href="/doc/tutorial/create-module.html" >< Create a Go module</a > - <a class="Navigation-next" href="handle-errors.html" + <a class="Navigation-next" href="/doc/tutorial/handle-errors.html" >Return and handle an error ></a > </p> diff --git a/_content/doc/tutorial/compile-install.html b/_content/doc/tutorial/compile-install.html index 5395ae51..4d596185 100644 --- a/_content/doc/tutorial/compile-install.html +++ b/_content/doc/tutorial/compile-install.html @@ -4,17 +4,27 @@ }--> <p> - In the last section, you'll learn a new <code>go</code> command. While the - <code>go run</code> command is a useful shortcut for compiling and running a - single-file program, it doesn't generate a binary executable you can easily - run again. If you want one of those, a good choice is to run the - <a - href="https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies" - ><code>go install</code> command</a - >, which compiles your code and installs the resulting binary executable where - you can run it. + In this last topic, you'll learn a couple new <code>go</code> commands. While + the <code>go run</code> command is a useful shortcut for compiling and running + a program when you're making frequent changes, it doesn't generate a binary + executable.</p> + +<p> + This topic introduces two additional commands for building code: </p> +<ul> + <li> + The <a href="/cmd/go/#hdr-Compile_packages_and_dependencies"><code>go + build</code> command</a> compiles the packages, along with their dependencies, + but it doesn't install the results. + </li> + <li> + The <a href="/ref/mod#go-install"><code>go + install</code> command</a> compiles and installs the packages. + </li> +</ul> + <aside class="Note"> <strong>Note:</strong> This topic is part of a multi-part tutorial that begins with <a href="create-module.html">Create a Go module</a>. @@ -22,7 +32,49 @@ <ol> <li> - At the command line, change to the directory that contains hello/hello.go. + From the command line in the hello directory, run the <code>go build</code> + command to compile the code into an executable. + + <pre>$ go build</pre> + </li> + + <li> + From the command line in the hello directory, run the new <code>hello</code> + executable to confirm that the code works. + + <p> + Note that your result might differ depending on whether you changed + your greetings.go code after testing it. + </p> + + <ul> + <li> + On Linux or Mac: + + <pre> +$ ./hello +map[Darrin:Great to see you, Darrin! Gladys:Hail, Gladys! Well met! Samantha:Hail, Samantha! Well met!] +</pre> + </li> + + <li> + On Windows: + + <pre> +$ hello.exe +map[Darrin:Great to see you, Darrin! Gladys:Hail, Gladys! Well met! Samantha:Hail, Samantha! Well met!] +</pre> + </li> + </ul> + <p> + You've compiled the application into an executable so you can run it. + But to run it currently, your prompt needs either to be in the executable's + directory, or to specify the executable's path. + </p> + <p> + Next, you'll install the executable so you can run it without specifying + its path. + </p> </li> <li> @@ -31,21 +83,18 @@ <p> You can discover the install path by running the - <a href="https://golang.org/cmd/go/#hdr-List_packages_or_modules" - ><code>go list</code> command</a - >, as in the following example: + <a href="/cmd/go/#hdr-List_packages_or_modules"> + <code>go list</code> command</a>, as in the following example: </p> <pre> -go list -f '{{.Target}}' -</pre - > +$ go list -f '{{.Target}}' +</pre> <p> - For example, the command's output might say - <code>/home/gopher/bin/hello</code>, meaning that binaries are installed - to /home/gopher/bin. This is the install directory you'll need in the next - step. + For example, the command's output might say <code>/home/gopher/bin/hello</code>, + meaning that binaries are installed to /home/gopher/bin. You'll need this + install directory in the next step. </p> </li> @@ -62,7 +111,7 @@ go list -f '{{.Target}}' On Linux or Mac, run the following command: <pre> -export PATH=$PATH:/path/to/your/install/directory +$ export PATH=$PATH:/path/to/your/install/directory </pre > </li> @@ -71,7 +120,7 @@ export PATH=$PATH:/path/to/your/install/directory On Windows, run the following command: <pre> -set PATH=%PATH%;C:\path\to\your\install\directory +$ set PATH=%PATH%;C:\path\to\your\install\directory </pre > </li> @@ -80,24 +129,22 @@ set PATH=%PATH%;C:\path\to\your\install\directory <p> As an alternative, if you already have a directory like <code>$HOME/bin</code> in your shell path and you'd like to install your - Go programs there, you can change the install target by setting the GOBIN - variable using the - <a href="https://golang.org/cmd/go/#hdr-Print_Go_environment_information" - ><code>go env</code> command</a - >: + Go programs there, you can change the install target by setting the + <code>GOBIN</code> variable using the + <a href="/cmd/go/#hdr-Print_Go_environment_information"> + <code>go env</code> command</a>: </p> <pre> -go env -w GOBIN=/path/to/your/bin -</pre - > +$ go env -w GOBIN=/path/to/your/bin +</pre> <p> or </p> <pre> -go env -w GOBIN=C:\path\to\your\bin +$ go env -w GOBIN=C:\path\to\your\bin </pre > </li> @@ -106,14 +153,13 @@ go env -w GOBIN=C:\path\to\your\bin Once you've updated the shell path, run the <code>go install</code> command to compile and install the package. - <pre> -$ go install -</pre - > + <pre>$ go install</pre> </li> <li> - Run your application by simply typing its name. + Run your application by simply typing its name. To make this interesting, + open a new command prompt and run the <code>hello</code> executable name + in some other directory. <pre> $ hello @@ -124,11 +170,11 @@ map[Darrin:Hail, Darrin! Well met! Gladys:Great to see you, Gladys! Samantha:Hai </ol> <p> - That wraps up this Go tutorial! For a next step that introduces many more of - Go features, check out the - <a href="https://tour.golang.org/welcome/1">Tour of Go</a>. + That wraps up this Go tutorial! </p> <p class="Navigation"> <a class="Navigation-prev" href="add-a-test.html">< Add a test</a> + <a class="Navigation-next" href="module-conclusion.html">Conclusion and links + to more information ></a> </p> diff --git a/_content/doc/tutorial/create-module.html b/_content/doc/tutorial/create-module.html index cf6558c0..3dbe35f4 100644 --- a/_content/doc/tutorial/create-module.html +++ b/_content/doc/tutorial/create-module.html @@ -6,8 +6,8 @@ <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 the - <a href="getting-started.html">getting started</a> tutorial, which introduces + 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> @@ -28,36 +28,36 @@ another module. </li> <li> - <a href="call-module-code.html">Call your code from another module</a> -- + <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="handle-errors.html">Return and handle an error</a> -- Add simple + <a href="/doc/tutorial/handle-errors.html">Return and handle an error</a> -- Add simple error handling. </li> <li> - <a href="random-greeting.html">Return a random greeting</a> -- Handle data + <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="greetings-multiple-people.html" + <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="add-a-test.html">Add a test</a> -- Use Go's built-in unit testing + <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="compile-install.html">Compile and install the application</a> -- + <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="index.html">Tutorials</a>. + <a href="/doc/tutorial/index.html">Tutorials</a>. </aside> <h2 id="prerequisites">Prerequisites</h2> @@ -81,18 +81,18 @@ <h2 id="start">Start a module that others can use</h2> <p> - Start by creating a - <a href="https://golang.org/doc/code.html#Organization">Go module</a>. In a + 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. + 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 - package's module specifies the context Go needs to run the code, including the - Go version the code is written for and the set of other modules it requires. + module specifies dependencies needed to run your code, including the Go + version and the set of other modules it requires. </p> <p> @@ -127,7 +127,6 @@ cd %HOMEPATH% <li> Create a <code>greetings</code> directory for your Go module source code. - This is where you'll write your module code. <p> For example, from your home directory use the following commands: @@ -143,16 +142,15 @@ cd greetings <li> Start your module using the <a - href="https://golang.org/cmd/go/#hdr-Initialize_new_module_in_current_directory" + href="/ref/mod#go-mod-init" ><code>go mod init</code> command</a - > - to create a go.mod file. + >. <p> - Run the <code>go mod init</code> command, giving it the path of the module - your code will be in. Here, use <code>example.com/greetings</code> for the - module path -- in production code, this would be the URL from which your - module can be downloaded. + 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> <pre> @@ -162,13 +160,12 @@ go: creating new go.mod: module example.com/greetings > <p> - The <code>go mod init</code> command creates a go.mod file that identifies - your code as a module that might be used from other code. The file you - just created includes only the name of your module and the Go version your - code supports. But as you add dependencies -- meaning packages from other - modules -- the go.mod file will list the specific module versions to use. - This keeps builds reproducible and gives you direct control over which - module versions to use. + 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> @@ -212,10 +209,12 @@ func Hello(name string) string { 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>, and 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 - <a href="https://tour.golang.org/basics/3"><em>exported</em> name</a>. + <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="https://tour.golang.org/basics/3">Exported names</a> in the + Go tour. </p> <img src="images/function-syntax.png" width="300px" /> </li> @@ -236,11 +235,12 @@ message = fmt.Sprintf("Hi, %v. Welcome!", name) </li> <li> - Use the <code>fmt</code> package's <code>Sprintf</code> function 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. + 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> @@ -248,12 +248,11 @@ message = fmt.Sprintf("Hi, %v. Welcome!", name) </ol> <p> - In the <a href="call-module-code.html">next step</a>, you'll call this - function from another module. + In the next step, you'll call this function from another module. </p> <p class="Navigation"> - <a class="Navigation-next" href="call-module-code.html" + <a class="Navigation-next" href="/doc/tutorial/call-module-code.html" >Call your code from another module ></a > </p> diff --git a/_content/doc/tutorial/getting-started.html b/_content/doc/tutorial/getting-started.html index fc05867f..eff1bb69 100644 --- a/_content/doc/tutorial/getting-started.html +++ b/_content/doc/tutorial/getting-started.html @@ -21,7 +21,7 @@ <aside class="Note"> <strong>Note:</strong> For other tutorials, see - <a href="index.html">Tutorials</a>. + <a href="/doc/tutorial/index.html">Tutorials</a>. </aside> <h2 id="prerequisites">Prerequisites</h2> @@ -90,26 +90,35 @@ cd hello </li> <li> - Initialize a new module for tracking dependencies. + Enable dependency tracking for your code. <p> - When your code imports packages from another module, a go.mod file lists - the specific modules and versions providing those packages. That file - stays with your code, including in your source code repository. + When your code imports packages contained in other modules, you manage + those dependencies through your code's own module. That module is defined + by a go.mod file that tracks the modules that provide those packages. That + go.mod file stays with your code, including in your source code + repository. </p> <p> - To create a go.mod file, run the - <a - href="https://golang.org/cmd/go/#hdr-Initialize_new_module_in_current_directory" - ><code>go mod init</code> command</a - >, giving it the name of the module your code will be in (here, just use - "hello"): + To enable dependency tracking for your code by creating a go.mod file, run + the + <a href="/ref/mod#go-mod-init"><code>go mod init</code> command</a>, + giving it the name of the module your code will be in. The name is the + module's module path. In most cases, this will be the repository + location where your source code will be kept, such as + <code>github.com/mymodule</code>. If you plan to publish your module + for others to use, the module path <em>must</em> be a location from + which Go tools can download your module. + </p> + + <p>For the purposes of this tutorial, just use + <code>example.com/hello</code>. </p> <pre> -$ go mod init hello -go: creating new go.mod: module hello +$ go mod init example.com/hello +go: creating new go.mod: module example.com/hello </pre > </li> @@ -143,10 +152,10 @@ func main() { </li> <li> Import the popular - <a href="https://golang.org/pkg/fmt/"><code>fmt</code> package</a>, + <a href="https://pkg.go.dev/fmt/"><code>fmt</code> package</a>, which contains functions for formatting text, including printing to the console. This package is one of the - <a href="https://golang.org/pkg/">standard library</a> packages you got + <a href="https://pkg.go.dev/std">standard library</a> packages you got when you installed Go. </li> <li> @@ -168,7 +177,7 @@ Hello, World! <p> The - <a href="https://golang.org/cmd/go/#hdr-Compile_and_run_Go_program" + <a href="/cmd/go/#hdr-Compile_and_run_Go_program" ><code>go run</code> command</a > is one of many <code>go</code> commands you'll use to get things done with @@ -252,7 +261,10 @@ func main() { Add new module requirements and sums. <p> - Go will add the <code>quote</code> module as a requirement, as well as a go.sum file for use in authenticating the module. For more, see <a href="https://golang.org/cmd/go/#hdr-Module_authentication_using_go_sum">Module authentication using go.sum</a>. + Go will add the <code>quote</code> module as a requirement, as well as a + go.sum file for use in authenticating the module. For more, see + <a href="/ref/mod#authenticating">Authenticating modules</a> in the Go + Modules Reference. </p> <pre> $ go mod tidy @@ -289,5 +301,5 @@ Don't communicate by sharing memory, share memory by communicating. <p> With this quick introduction, you got Go installed and learned some of the basics. To write some more code with another tutorial, take a look at - <a href="create-module.html">Create a Go module</a>. + <a href="/doc/tutorial/create-module.html">Create a Go module</a>. </p> diff --git a/_content/doc/tutorial/greetings-multiple-people.html b/_content/doc/tutorial/greetings-multiple-people.html index 8100c080..1d83192d 100644 --- a/_content/doc/tutorial/greetings-multiple-people.html +++ b/_content/doc/tutorial/greetings-multiple-people.html @@ -6,33 +6,27 @@ <p> In the last changes you'll make to your module's code, you'll add support for getting greetings for multiple people in one request. In other words, you'll - handle a multiple-value input and pair values with a multiple-value output. + handle a multiple-value input, then pair values in that input with a + multiple-value output. To do this, you'll need to pass a set of names to a + function that can return a greeting for each of them. </p> <aside class="Note"> <strong>Note:</strong> This topic is part of a multi-part tutorial that begins - with <a href="create-module.html">Create a Go module</a>. + with <a href="/doc/tutorial/create-module.html">Create a Go module</a>. </aside> <p> - To do this, you'll need to pass a set of names to a function that can return a - greeting for each of them. Changing the <code>Hello</code> function's - parameter from a single name to a set of names would change the function - signature. If you had already published the <code>greetings</code> module and - users had already written code calling <code>Hello</code>, that change would - break their programs. In this situation, a better choice is to give new - functionality a new name. -</p> + But there's a hitch. Changing the <code>Hello</code> function's + parameter from a single name to a set of names would change the function's + signature. If you had already published the <code>example.com/greetings</code> + module and users had already written code calling <code>Hello</code>, that + change would break their programs.</p> <p> - In the last code you'll add with this tutorial, update the code as if you've - already published a version of the <code>greetings</code> module. Instead of - changing the <code>Hello</code> function, add a new function - <code>Hellos</code> that takes a set of names. Then, for the sake of - simplicity, have the new function call the existing one. Keeping both - functions in the package leaves the original for existing callers (or future - callers who only need one greeting) and adds a new one for callers that want - the expanded functionality. + In this situation, a better choice is to write a new function with a different + name. The new function will take multiple parameters. That preserves the old + function for backward compatibility. </p> <ol> @@ -111,27 +105,28 @@ func randomFormat() string { mapped to greeting messages. </li> <li> - Have the new Hellos function call the existing Hello function. This - leaves both functions in place. + Have the new <code>Hellos</code> function call the existing + <code>Hello</code> function. This helps reduce duplication while also + leaving both functions in place. </li> <li> - Create a <code>messages</code> - <a href="https://blog.golang.org/maps">map</a> to associate each of the + Create a <code>messages</code> map to associate each of the received names (as a key) with a generated message (as a value). In Go, you initialize a map with the following syntax: <code>make(map[<em>key-type</em>]<em>value-type</em>)</code>. You have - the <code>Hellos</code> function return this map to the caller. + the <code>Hellos</code> function return this map to the caller. For more + about maps, see <a href="https://blog.golang.org/maps">Go maps in + action</a> on the Go blog. </li> <li> Loop through the names your function received, checking that each has a non-empty value, then associate a message with each. In this <code>for</code> loop, <code>range</code> returns two values: the index of the current item in the loop and a copy of the item's value. You - don't need the index, so you use the Go - <a href="https://golang.org/doc/effective_go.html#blank" - >blank identifier (an underscore)</a - > - to ignore it. + don't need the index, so you use the Go blank identifier (an underscore) + to ignore it. For more, see + <a href="/doc/effective_go.html#blank">The blank + identifier</a> in Effective Go. </li> </ul> </li> @@ -193,7 +188,7 @@ func main() { <li> At the command line, change to the directory that contains hello/hello.go, - then run hello.go to confirm that the code works. + then use <code>go run</code> to confirm that the code works. <p> The output should be a string representation of the map associating names @@ -201,7 +196,7 @@ func main() { </p> <pre> -$ go run hello.go +$ go run . map[Darrin:Hail, Darrin! Well met! Gladys:Hi, Gladys. Welcome! Samantha:Hail, Samantha! Well met!] </pre > @@ -210,18 +205,18 @@ map[Darrin:Hail, Darrin! Well met! Gladys:Hi, Gladys. Welcome! Samantha:Hail, Sa <p> This topic introduced maps for representing name/value pairs. It also - introduced the idea of - <a href="https://blog.golang.org/module-compatibility" - >preserving backward compatibility</a - > + introduced the idea of preserving backward compatibility by implementing a new function for new or changed functionality in a module. - In the tutorial's <a href="add-a-test.html">next topic</a>, you'll use - built-in features to create a unit test for your code. + For more about backward compatibility, see + <a href="https://blog.golang.org/module-compatibility">Keeping your modules + compatible</a>. </p> +<p>Next, you'll use built-in Go features to create a unit test for your code.</p> + <p class="Navigation"> - <a class="Navigation-prev" href="random-greeting.html" + <a class="Navigation-prev" href="/doc/tutorial/random-greeting.html" >< Return a random greeting</a > - <a class="Navigation-next" href="add-a-test.html">Add a test ></a> + <a class="Navigation-next" href="/doc/tutorial/add-a-test.html">Add a test ></a> </p> diff --git a/_content/doc/tutorial/handle-errors.html b/_content/doc/tutorial/handle-errors.html index 4dfd4b13..76f7a4f3 100644 --- a/_content/doc/tutorial/handle-errors.html +++ b/_content/doc/tutorial/handle-errors.html @@ -11,7 +11,7 @@ <aside class="Note"> <strong>Note:</strong> This topic is part of a multi-part tutorial that begins - with <a href="create-module.html">Create a Go module</a>. + with <a href="/doc/tutorial/create-module.html">Create a Go module</a>. </aside> <ol> @@ -55,14 +55,13 @@ func Hello(name string) <ins>(</ins>string<ins>, error)</ins> { Change the function so that it returns two values: a <code>string</code> and an <code>error</code>. Your caller will check the second value to see if an error occurred. (Any Go function can - <a href="https://golang.org/doc/effective_go.html#multiple-returns" - >return multiple values</a - >.) + return multiple values. For more, see + <a href="/doc/effective_go.html#multiple-returns">Effective Go</a>.) </li> <li> Import the Go standard library <code>errors</code> package so you can use its - <a href="https://golang.org/pkg/errors/#example_New" + <a href="https://pkg.go.dev/errors/#example-New" ><code>errors.New</code> function</a >. </li> @@ -106,7 +105,7 @@ func main() { log.SetFlags(0)</ins> // Request a greeting message. - message, err := greetings.Hello("") + <ins>message, err := greetings.Hello("")</ins> <ins>// If an error was returned, print it to the console and // exit the program. if err != nil { @@ -126,7 +125,7 @@ func main() { <ul> <li> Configure the - <a href="https://golang.org/pkg/log/"><code>log</code> package</a> to + <a href="https://pkg.go.dev/log/"><code>log</code> package</a> to print the command name ("greetings: ") at the start of its log messages, without a time stamp or source file information. </li> @@ -163,7 +162,7 @@ func main() { </p> <pre> -$ go run hello.go +$ go run . greetings: empty name exit status 1 </pre @@ -172,17 +171,19 @@ exit status 1 </ol> <p> - That's essentially how error handling in Go works: Return an error as a value - so the caller can check for it. It's pretty simple. In the tutorial's - <a href="random-greeting.html">next topic</a>, you'll use a Go slice to return - a randomly-selected greeting. + That's common error handling in Go: Return an error as a value so the caller + can check for it. +</p> + +<p> + Next, you'll use a Go slice to return a randomly-selected greeting. </p> <p class="Navigation"> - <a class="Navigation-prev" href="call-module-code.html" + <a class="Navigation-prev" href="/doc/tutorial/call-module-code.html" >< Call your code from another module</a > - <a class="Navigation-next" href="random-greeting.html" + <a class="Navigation-next" href="/doc/tutorial/random-greeting.html" >Return a random greeting ></a > </p> diff --git a/_content/doc/tutorial/index.html b/_content/doc/tutorial/index.html index 80f9c018..1bbb78d0 100644 --- a/_content/doc/tutorial/index.html +++ b/_content/doc/tutorial/index.html @@ -20,13 +20,13 @@ <tbody> <tr class="DocTable-row"> <td class="DocTable-cell"> - <a href="getting-started.html">Getting started</a> + <a href="/doc/tutorial/getting-started.html">Getting started</a> </td> <td class="DocTable-cell">Say Hello, World with Go.</td> </tr> <tr class="DocTable-row"> <td class="DocTable-cell"> - <a href="create-module.html">Create a module</a> + <a href="/doc/tutorial/create-module.html">Create a module</a> </td> <td class="DocTable-cell"> A multi-part tutorial that introduces common programming language @@ -38,7 +38,8 @@ <a href="https://tour.golang.org/welcome/1">A Tour of Go</a> </td> <td class="DocTable-cell"> - An interactive introduction to Go: basic syntax and data structures; methods and interfaces; and Go's concurrency primitives. + An interactive introduction to Go: basic syntax and data structures; + methods and interfaces; and Go's concurrency primitives. </td> </tr> </tbody> diff --git a/_content/doc/tutorial/module-conclusion.html b/_content/doc/tutorial/module-conclusion.html new file mode 100644 index 00000000..f86af3af --- /dev/null +++ b/_content/doc/tutorial/module-conclusion.html @@ -0,0 +1,31 @@ +<!--{ + "Title": "Conclusion", + "Path": "/doc/tutorial/module-conclusion" +}--> + +<p> + In this tutorial, you wrote functions that you packaged into two modules: one + with logic for sending greetings; the other as a consumer for the first. +</p> + +<aside class="Note"> + <strong>Note:</strong> This topic is part of a multi-part tutorial that begins + with <a href="/doc/tutorial/create-module.html">Create a Go module</a>. +</aside> + +<p> + For more on managing dependencies in your code, see + <a href="/doc/modules/managing-dependencies">Managing dependencies</a>. For + more about developing modules for others to use, see + <a href="/doc/modules/developing">Developing and publishing modules</a>. +</p> + +<p>For many more features of the Go language, check out the + <a href="https://tour.golang.org/welcome/1">Tour of Go</a>. +</p> + +<p class="Navigation"> + <a class="Navigation-prev" href="/doc/tutorial/compile-install.html" + >< Compile and install the application</a + > +</p> diff --git a/_content/doc/tutorial/random-greeting.html b/_content/doc/tutorial/random-greeting.html index d5a45be6..c6c8da72 100644 --- a/_content/doc/tutorial/random-greeting.html +++ b/_content/doc/tutorial/random-greeting.html @@ -10,15 +10,20 @@ <aside class="Note"> <strong>Note:</strong> This topic is part of a multi-part tutorial that begins - with <a href="create-module.html">Create a Go module</a>. + with <a href="/doc/tutorial/create-module.html">Create a Go module</a>. </aside> <p> - To do this, you'll use a Go slice. A - <a href="https://blog.golang.org/slices-intro"><em>slice</em></a> is like an - array, except that it's dynamically sized as you add and remove items. It's - one of the most useful types in Go. You'll add a small slice to contain three - greeting messages, then have your code return one of the messages randomly. + To do this, you'll use a Go slice. A slice is like an array, except that its + size changes dynamically as you add and remove items. The slice is one of Go's + most useful types. +</p> + +<p> + You'll add a small slice to contain three greeting messages, then have your + code return one of the messages randomly. For more on slices, + see <a href="https://blog.golang.org/slices-intro">Go slices</a> in the Go + blog. </p> <ol> @@ -82,24 +87,22 @@ func randomFormat() string { <li> In <code>randomFormat</code>, declare a <code>formats</code> slice with three message formats. When declaring a slice, you omit its size in the - brackets, like this: <code>[]string</code>. This tells Go that the array - underlying a slice can be dynamically sized. + brackets, like this: <code>[]string</code>. This tells Go that the size + of the array underlying the slice can be dynamically changed. </li> <li> Use the - <a href="https://golang.org/pkg/math/rand/" + <a href="https://pkg.go.dev/math/rand/" ><code>math/rand</code> package</a > to generate a random number for selecting an item from the slice. </li> <li> - Add an - <a href="https://golang.org/doc/effective_go.html#init" - ><code>init</code> function</a - > - to seed the <code>rand</code> package with the current time. Go executes - <code>init</code> functions automatically at program startup, after - global variables have been initialized. + Add an <code>init</code> function to seed the <code>rand</code> package + with the current time. Go executes <code>init</code> functions + automatically at program startup, after global variables have been + initialized. For more about <code>init</code> functions, see + <a href="/doc/effective_go.html#init">Effective Go</a>. </li> <li> In <code>Hello</code>, call the <code>randomFormat</code> function to @@ -109,48 +112,70 @@ func randomFormat() string { <li>Return the message (or an error) as you did before.</li> </ul> - <p> - Your hello.go needn't change. - </p> </li> <li> - At the command line, change to the hello directory, then run hello.go to - confirm that the code works. Run it multiple times, noticing that the - greeting changes. + In hello/hello.go, change your code so it looks like the following. - <p> - Oh -- don't forget to add Gladys's name (or a different name, if you like) - as an argument to the <code>Hello</code> function call in hello.go: - <code>greetings.Hello("Gladys")</code> - </p> + <p>You're just adding Gladys's name (or a different name, if you like) + as an argument to the <code>Hello</code> function call in hello.go.</p> + + <pre>package main + +import ( + "fmt" + "log" + + "example.com/greetings" +) + +func main() { + // Set properties of the predefined Logger, including + // the log entry prefix and a flag to disable printing + // the time, source file, and line number. + log.SetPrefix("greetings: ") + log.SetFlags(0) + + // Request a greeting message. + <ins>message, err := greetings.Hello("Gladys")</ins> + // If an error was returned, print it to the console and + // exit the program. + if err != nil { + log.Fatal(err) + } + + // If no error was returned, print the returned message + // to the console. + fmt.Println(message) +}</pre> + </li> + + <li> + At the command line, in the hello directory, run hello.go to confirm that + the code works. Run it multiple times, noticing that the greeting changes. <pre> -$ go build -$ ./hello +$ go run . Great to see you, Gladys! -$ ./hello +$ go run . Hi, Gladys. Welcome! -$ ./hello +$ go run . Hail, Gladys! Well met! -</pre - > +</pre> </li> </ol> <p> - That's an introduction to a Go slice. To get even more use out of this type, - you'll use a slice to greet multiple people. That's in the tutorial's - <a href="greetings-multiple-people.html">next topic</a>. + Next, you'll use a slice to greet multiple people. </p> <p class="Navigation"> - <a class="Navigation-prev" href="handle-errors.html" + <a class="Navigation-prev" href="/doc/tutorial/handle-errors.html" >< Return and handle an error</a > - <a class="Navigation-next" href="greetings-multiple-people.html" + <a class="Navigation-next" href="/doc/tutorial/greetings-multiple-people.html" >Return greetings for multiple people ></a > </p> |
