From e7b262c538518b210432d76bce99d358c72cead3 Mon Sep 17 00:00:00 2001
From: Baokun Lee
+ 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.
+
+ To create a go.mod file, run the
+
- 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.
+ Go will add the
- To create a go.mod file, run the
- go mod init
command, giving it the name of the module your code will be in (here, just use
+ "hello"):
+
+$ go mod init hello
+go: creating new go.mod: module hello
+
+ quote
module as a requirement, as well as a go.sum file for use in authenticating the module. For more, see Module authentication using go.sum.
go mod init
command, giving it the name of the module your code will be in (here, just use
- "hello"):
-
-$ go mod init hello
-go: creating new go.mod: module hello
+$ go mod tidy
+go: finding module for package rsc.io/quote
+go: found rsc.io/quote in rsc.io/quote v1.5.2
$ go run .
-go: finding module for package rsc.io/quote
-go: found rsc.io/quote in rsc.io/quote v1.5.2
Don't communicate by sharing memory, share memory by communicating.
@@ -265,10 +277,9 @@ Don't communicate by sharing memory, share memory by communicating.
- But before it ran the code, go run
located and downloaded the
+ When you ran go mod tidy
, it located and downloaded the
rsc.io/quote
module that contains the package you imported.
- By default, it downloaded the latest version -- v1.5.2. Go build commands
- are designed to locate the modules required for packages you import.
+ By default, it downloaded the latest version -- v1.5.2.
\x0a\x20\x20This\x20is\x20the\x20first\x20part\x20of\x20a\x20tutorial\x20that\x20introduces\x20a\x20few\x20fundamental\x0a\x20\x20features\x20of\x20the\x20Go\x20language.\x20If\x20you're\x20just\x20getting\x20started\x20with\x20Go,\x20be\x20sure\x0a\x20\x20to\x20take\x20a\x20look\x20at\x20the\x0a\x20\x20getting\x20started\x20tutorial,\x20which\x20introduces\x0a\x20\x20the\x20go
\x20command,\x20Go\x20modules,\x20and\x20very\x20simple\x20Go\x20code.\x0a
\x0a\x20\x20In\x20this\x20tutorial\x20you'll\x20create\x20two\x20modules.\x20The\x20first\x20is\x20a\x20library\x20which\x20is\x0a\x20\x20intended\x20to\x20be\x20imported\x20by\x20other\x20libraries\x20or\x20applications.\x20The\x20second\x20is\x20a\x0a\x20\x20caller\x20application\x20which\x20will\x20use\x20the\x20first.\x0a
\x0a\x0a\x0a\x20\x20This\x20tutorial's\x20sequence\x20includes\x20six\x20brief\x20topics\x20that\x20each\x20illustrate\x20a\x0a\x20\x20different\x20part\x20of\x20the\x20language.\x0a
\x0a\x0a\x0a\x20\x20Start\x20by\x20creating\x20a\x0a\x20\x20Go\x20module.\x20In\x20a\x0a\x20\x20module,\x20you\x20collect\x20one\x20or\x20more\x20related\x20packages\x20for\x20a\x20discrete\x20and\x20useful\x20set\x0a\x20\x20of\x20functions.\x20For\x20example,\x20you\x20might\x20create\x20a\x20module\x20with\x20packages\x20that\x20have\x0a\x20\x20functions\x20for\x20doing\x20financial\x20analysis\x20so\x20that\x20others\x20writing\x20financial\x0a\x20\x20applications\x20can\x20use\x20your\x20work.\x0a
\x0a\x0a\x0a\x20\x20Go\x20code\x20is\x20grouped\x20into\x20packages,\x20and\x20packages\x20are\x20grouped\x20into\x20modules.\x20Your\x0a\x20\x20package's\x20module\x20specifies\x20the\x20context\x20Go\x20needs\x20to\x20run\x20the\x20code,\x20including\x20the\x0a\x20\x20Go\x20version\x20the\x20code\x20is\x20written\x20for\x20and\x20the\x20set\x20of\x20other\x20modules\x20it\x20requires.\x0a
\x0a\x0a\x0a\x20\x20As\x20you\x20add\x20or\x20improve\x20functionality\x20in\x20your\x20module,\x20you\x20publish\x20new\x20versions\x0a\x20\x20of\x20the\x20module.\x20Developers\x20writing\x20code\x20that\x20calls\x20functions\x20in\x20your\x20module\x20can\x0a\x20\x20import\x20the\x20module's\x20updated\x20packages\x20and\x20test\x20with\x20the\x20new\x20version\x20before\x0a\x20\x20putting\x20it\x20into\x20production\x20use.\x0a
\x0a\x0acd
\x20to\x20your\x20home\x20directory.\x0a\x0a\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20On\x20Linux\x20or\x20Mac:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0acd\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20On\x20Windows:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0acd\x20%HOMEPATH%\x0a\x0a\x20\x20
greetings
\x20directory\x20for\x20your\x20Go\x20module\x20source\x20code.\x0a\x20\x20\x20\x20This\x20is\x20where\x20you'll\x20write\x20your\x20module\x20code.\x0a\x0a\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20For\x20example,\x20from\x20your\x20home\x20directory\x20use\x20the\x20following\x20commands:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0amkdir\x20greetings\x0acd\x20greetings\x0a\x0a\x20\x20
go\x20mod\x20init
\x20command\x0a\x20\x20\x20\x20to\x20create\x20a\x20go.mod\x20file.\x0a\x0a\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20Run\x20the\x20go\x20mod\x20init
\x20command,\x20giving\x20it\x20the\x20path\x20of\x20the\x20module\x0a\x20\x20\x20\x20\x20\x20your\x20code\x20will\x20be\x20in.\x20Here,\x20use\x20example.com/greetings
\x20for\x20the\x0a\x20\x20\x20\x20\x20\x20module\x20path\x20--\x20in\x20production\x20code,\x20this\x20would\x20be\x20the\x20URL\x20from\x20which\x20your\x0a\x20\x20\x20\x20\x20\x20module\x20can\x20be\x20downloaded.\x0a\x20\x20\x20\x20
\x0a$\x20go\x20mod\x20init\x20example.com/greetings\x0ago:\x20creating\x20new\x20go.mod:\x20module\x20example.com/greetings\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20The\x20go\x20mod\x20init
\x20command\x20creates\x20a\x20go.mod\x20file\x20that\x20identifies\x0a\x20\x20\x20\x20\x20\x20your\x20code\x20as\x20a\x20module\x20that\x20might\x20be\x20used\x20from\x20other\x20code.\x20The\x20file\x20you\x0a\x20\x20\x20\x20\x20\x20just\x20created\x20includes\x20only\x20the\x20name\x20of\x20your\x20module\x20and\x20the\x20Go\x20version\x20your\x0a\x20\x20\x20\x20\x20\x20code\x20supports.\x20But\x20as\x20you\x20add\x20dependencies\x20--\x20meaning\x20packages\x20from\x20other\x0a\x20\x20\x20\x20\x20\x20modules\x20--\x20the\x20go.mod\x20file\x20will\x20list\x20the\x20specific\x20module\x20versions\x20to\x20use.\x0a\x20\x20\x20\x20\x20\x20This\x20keeps\x20builds\x20reproducible\x20and\x20gives\x20you\x20direct\x20control\x20over\x20which\x0a\x20\x20\x20\x20\x20\x20module\x20versions\x20to\x20use.\x0a\x20\x20\x20\x20
\x0apackage\x20greetings\x0a\x0aimport\x20\"fmt\"\x0a\x0a//\x20Hello\x20returns\x20a\x20greeting\x20for\x20the\x20named\x20person.\x0afunc\x20Hello(name\x20string)\x20string\x20{\x0a\x20\x20\x20\x20//\x20Return\x20a\x20greeting\x20that\x20embeds\x20the\x20name\x20in\x20a\x20message.\x0a\x20\x20\x20\x20message\x20:=\x20fmt.Sprintf(\"Hi,\x20%v.\x20Welcome!\",\x20name)\x0a\x20\x20\x20\x20return\x20message\x0a}\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20This\x20is\x20the\x20first\x20code\x20for\x20your\x20module.\x20It\x20returns\x20a\x20greeting\x20to\x20any\x0a\x20\x20\x20\x20\x20\x20caller\x20that\x20asks\x20for\x20one.\x20You'll\x20write\x20code\x20that\x20calls\x20this\x20function\x20in\x0a\x20\x20\x20\x20\x20\x20the\x20next\x20step.\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20In\x20this\x20code,\x20you:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20greetings
\x20package\x20to\x20collect\x20related\x20functions.\x0a\x20\x20\x20\x20\x20\x20Hello
\x20function\x20to\x20return\x20the\x20greeting.\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20This\x20function\x20takes\x20a\x20name
\x20parameter\x20whose\x20type\x20is\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20string
,\x20and\x20returns\x20a\x20string
.\x20In\x20Go,\x20a\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20function\x20whose\x20name\x20starts\x20with\x20a\x20capital\x20letter\x20can\x20be\x20called\x20by\x20a\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20function\x20not\x20in\x20the\x20same\x20package.\x20This\x20is\x20known\x20in\x20Go\x20as\x20an\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20exported\x20name.\x0a\x20\x20\x20\x20\x20\x20\x20\x20
message
\x20variable\x20to\x20hold\x20your\x20greeting.\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20In\x20Go,\x20the\x20:=
\x20operator\x20is\x20a\x20shortcut\x20for\x20declaring\x20and\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20initializing\x20a\x20variable\x20in\x20one\x20line\x20(Go\x20uses\x20the\x20value\x20on\x20the\x20right\x20to\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20determine\x20the\x20variable's\x20type).\x20Taking\x20the\x20long\x20way,\x20you\x20might\x20have\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20written\x20this\x20as:\x0a\x20\x20\x20\x20\x20\x20\x20\x20
\x0avar\x20message\x20string\x0amessage\x20=\x20fmt.Sprintf(\"Hi,\x20%v.\x20Welcome!\",\x20name)\x0a\x0a\x20\x20\x20\x20\x20\x20
fmt
\x20package's\x20Sprintf
\x20function\x20to\x0a\x20\x20\x20\x20\x20\x20\x20\x20create\x20a\x20greeting\x20message.\x20The\x20first\x20argument\x20is\x20a\x20format\x20string,\x20and\x0a\x20\x20\x20\x20\x20\x20\x20\x20Sprintf
\x20substitutes\x20the\x20name
\x20parameter's\x20value\x0a\x20\x20\x20\x20\x20\x20\x20\x20for\x20the\x20%v
\x20format\x20verb.\x20Inserting\x20the\x20value\x20of\x20the\x0a\x20\x20\x20\x20\x20\x20\x20\x20name
\x20parameter\x20completes\x20the\x20greeting\x20text.\x0a\x20\x20\x20\x20\x20\x20\x0a\x20\x20In\x20the\x20next\x20step,\x20you'll\x20call\x20this\x0a\x20\x20function\x20from\x20another\x20module.\x0a
\x0a\x0a\x0a\x20\x20Call\x20your\x20code\x20from\x20another\x20module\x20>\x0a
\x0a", - "doc/tutorial/getting-started.html": "\x0a\x0a\x0a\x20\x20In\x20this\x20tutorial,\x20you'll\x20get\x20a\x20brief\x20introduction\x20to\x20Go\x20programming.\x20Along\x20the\x0a\x20\x20way,\x20you\x20will:\x0a
\x0a\x0ago
\x20command\x20to\x20run\x20your\x20code.Just\x20use\x20the\x20Download\x20and\x20install\x20steps.
\x0a\x0a\x0a\x20\x20Get\x20started\x20with\x20Hello,\x20World.\x0a
\x0a\x0a\x0a\x20\x20\x20\x20\x20\x20On\x20Linux\x20or\x20Mac:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0acd\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20On\x20Windows:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0acd\x20%HOMEPATH%\x0a\x0a\x20\x20
\x0a\x20\x20\x20\x20\x20\x20For\x20example,\x20use\x20the\x20following\x20commands:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0amkdir\x20hello\x0acd\x20hello\x0a\x0a\x20\x20
\x0apackage\x20main\x0a\x0aimport\x20\"fmt\"\x0a\x0afunc\x20main()\x20{\x0a\x20\x20\x20\x20fmt.Println(\"Hello,\x20World!\")\x0a}\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20This\x20is\x20your\x20Go\x20code.\x20In\x20this\x20code,\x20you:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20main
\x20package\x20(a\x20package\x20is\x20a\x20way\x20to\x20group\x0a\x20\x20\x20\x20\x20\x20\x20\x20functions,\x20and\x20it's\x20made\x20up\x20of\x20all\x20the\x20files\x20in\x20the\x20same\x20directory).\x0a\x20\x20\x20\x20\x20\x20fmt
\x20package,\x0a\x20\x20\x20\x20\x20\x20\x20\x20which\x20contains\x20functions\x20for\x20formatting\x20text,\x20including\x20printing\x20to\x20the\x0a\x20\x20\x20\x20\x20\x20\x20\x20console.\x20This\x20package\x20is\x20one\x20of\x20the\x0a\x20\x20\x20\x20\x20\x20\x20\x20standard\x20library\x20packages\x20you\x20got\x0a\x20\x20\x20\x20\x20\x20\x20\x20when\x20you\x20installed\x20Go.\x0a\x20\x20\x20\x20\x20\x20main
\x20function\x20to\x20print\x20a\x20message\x20to\x20the\x0a\x20\x20\x20\x20\x20\x20\x20\x20console.\x20A\x20main
\x20function\x20executes\x20by\x20default\x20when\x20you\x20run\x0a\x20\x20\x20\x20\x20\x20\x20\x20the\x20main
\x20package.\x0a\x20\x20\x20\x20\x20\x20\x0a$\x20go\x20run\x20.\x0aHello,\x20World!\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20The\x0a\x20\x20\x20\x20\x20\x20go\x20run
\x20command\x0a\x20\x20\x20\x20\x20\x20is\x20one\x20of\x20many\x20go
\x20commands\x20you'll\x20use\x20to\x20get\x20things\x20done\x20with\x0a\x20\x20\x20\x20\x20\x20Go.\x20Use\x20the\x20following\x20command\x20to\x20get\x20a\x20list\x20of\x20the\x20others:\x0a\x20\x20\x20\x20
\x0a$\x20go\x20help\x0a\x0a\x20\x20
\x0a\x20\x20When\x20you\x20need\x20your\x20code\x20to\x20do\x20something\x20that\x20might\x20have\x20been\x20implemented\x20by\x0a\x20\x20someone\x20else,\x20you\x20can\x20look\x20for\x20a\x20package\x20that\x20has\x20functions\x20you\x20can\x20use\x20in\x0a\x20\x20your\x20code.\x0a
\x0a\x0arsc.io/quote
\x20package\x20in\x20search\x20results\x0a\x20\x20\x20\x20\x20\x20\x20\x20(if\x20you\x20see\x20rsc.io/quote/v3
,\x20ignore\x20it\x20for\x20now).\x0a\x20\x20\x20\x20\x20\x20Go
\x20function.\x0a\x20\x20\x20\x20\x20\x20quote
\x20is\x0a\x20\x20\x20\x20\x20\x20\x20\x20included\x20in\x20the\x20rsc.io/quote
\x20module.\x0a\x20\x20\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20You\x20can\x20use\x20the\x20pkg.go.dev\x20site\x20to\x20find\x20published\x20modules\x20whose\x20packages\x0a\x20\x20\x20\x20\x20\x20have\x20functions\x20you\x20can\x20use\x20in\x20your\x20own\x20code.\x20Packages\x20are\x20published\x20in\x0a\x20\x20\x20\x20\x20\x20modules\x20--\x20like\x20rsc.io/quote
\x20--\x20where\x20others\x20can\x20use\x20them.\x0a\x20\x20\x20\x20\x20\x20Modules\x20are\x20improved\x20with\x20new\x20versions\x20over\x20time,\x20and\x20you\x20can\x20upgrade\x20your\x0a\x20\x20\x20\x20\x20\x20code\x20to\x20use\x20the\x20improved\x20versions.\x0a\x20\x20\x20\x20
rsc.io/quote
\x20package\x20and\x20add\x20a\x20call\x0a\x20\x20\x20\x20to\x20its\x20Go
\x20function.\x0a\x0a\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20After\x20adding\x20the\x20highlighted\x20lines,\x20your\x20code\x20should\x20include\x20the\x0a\x20\x20\x20\x20\x20\x20following:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0apackage\x20main\x0a\x0aimport\x20\"fmt\"\x0a\x0aimport\x20\"rsc.io/quote\"\x0a\x0afunc\x20main()\x20{\x0a\x20\x20\x20\x20fmt.Println(quote.Go())\x0a}\x0a\x0a\x20\x20
\x0a\x20\x20\x20\x20\x20\x20When\x20your\x20code\x20imports\x20packages\x20from\x20another\x20module,\x20a\x20go.mod\x20file\x20lists\x0a\x20\x20\x20\x20\x20\x20the\x20specific\x20modules\x20and\x20versions\x20providing\x20those\x20packages.\x20That\x20file\x0a\x20\x20\x20\x20\x20\x20stays\x20with\x20your\x20code,\x20including\x20in\x20your\x20source\x20code\x20repository.\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20To\x20create\x20a\x20go.mod\x20file,\x20run\x20the\x0a\x20\x20\x20\x20\x20\x20go\x20mod\x20init
\x20command,\x20giving\x20it\x20the\x20name\x20of\x20the\x20module\x20your\x20code\x20will\x20be\x20in\x20(here,\x20just\x20use\x0a\x20\x20\x20\x20\x20\x20\"hello\"):\x0a\x20\x20\x20\x20
\x0a$\x20go\x20mod\x20init\x20hello\x0ago:\x20creating\x20new\x20go.mod:\x20module\x20hello\x0a\x0a\x20\x20
\x0a$\x20go\x20run\x20.\x0ago:\x20finding\x20module\x20for\x20package\x20rsc.io/quote\x0ago:\x20found\x20rsc.io/quote\x20in\x20rsc.io/quote\x20v1.5.2\x0aDon't\x20communicate\x20by\x20sharing\x20memory,\x20share\x20memory\x20by\x20communicating.\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20Notice\x20that\x20your\x20code\x20calls\x20the\x20Go
\x20function,\x20printing\x20a\x0a\x20\x20\x20\x20\x20\x20clever\x20message\x20about\x20communication.\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20But\x20before\x20it\x20ran\x20the\x20code,\x20go\x20run
\x20located\x20and\x20downloaded\x20the\x0a\x20\x20\x20\x20\x20\x20rsc.io/quote
\x20module\x20that\x20contains\x20the\x20package\x20you\x20imported.\x0a\x20\x20\x20\x20\x20\x20By\x20default,\x20it\x20downloaded\x20the\x20latest\x20version\x20--\x20v1.5.2.\x20Go\x20build\x20commands\x0a\x20\x20\x20\x20\x20\x20are\x20designed\x20to\x20locate\x20the\x20modules\x20required\x20for\x20packages\x20you\x20import.\x0a\x20\x20\x20\x20
\x0a\x20\x20With\x20this\x20quick\x20introduction,\x20you\x20got\x20Go\x20installed\x20and\x20learned\x20some\x20of\x20the\x0a\x20\x20basics.\x20To\x20write\x20some\x20more\x20code\x20with\x20another\x20tutorial,\x20take\x20a\x20look\x20at\x0a\x20\x20Create\x20a\x20Go\x20module.\x0a
\x0a", + "doc/tutorial/getting-started.html": "\x0a\x0a\x0a\x20\x20In\x20this\x20tutorial,\x20you'll\x20get\x20a\x20brief\x20introduction\x20to\x20Go\x20programming.\x20Along\x20the\x0a\x20\x20way,\x20you\x20will:\x0a
\x0a\x0ago
\x20command\x20to\x20run\x20your\x20code.Just\x20use\x20the\x20Download\x20and\x20install\x20steps.
\x0a\x0a\x0a\x20\x20Get\x20started\x20with\x20Hello,\x20World.\x0a
\x0a\x0a\x0a\x20\x20\x20\x20\x20\x20On\x20Linux\x20or\x20Mac:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0acd\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20On\x20Windows:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0acd\x20%HOMEPATH%\x0a\x0a\x20\x20
\x0a\x20\x20\x20\x20\x20\x20For\x20example,\x20use\x20the\x20following\x20commands:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0amkdir\x20hello\x0acd\x20hello\x0a\x0a\x20\x20
\x0a\x20\x20\x20\x20\x20\x20When\x20your\x20code\x20imports\x20packages\x20from\x20another\x20module,\x20a\x20go.mod\x20file\x20lists\x0a\x20\x20\x20\x20\x20\x20the\x20specific\x20modules\x20and\x20versions\x20providing\x20those\x20packages.\x20That\x20file\x0a\x20\x20\x20\x20\x20\x20stays\x20with\x20your\x20code,\x20including\x20in\x20your\x20source\x20code\x20repository.\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20To\x20create\x20a\x20go.mod\x20file,\x20run\x20the\x0a\x20\x20\x20\x20\x20\x20go\x20mod\x20init
\x20command,\x20giving\x20it\x20the\x20name\x20of\x20the\x20module\x20your\x20code\x20will\x20be\x20in\x20(here,\x20just\x20use\x0a\x20\x20\x20\x20\x20\x20\"hello\"):\x0a\x20\x20\x20\x20
\x0a$\x20go\x20mod\x20init\x20hello\x0ago:\x20creating\x20new\x20go.mod:\x20module\x20hello\x0a\x0a\x20\x20
\x0apackage\x20main\x0a\x0aimport\x20\"fmt\"\x0a\x0afunc\x20main()\x20{\x0a\x20\x20\x20\x20fmt.Println(\"Hello,\x20World!\")\x0a}\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20This\x20is\x20your\x20Go\x20code.\x20In\x20this\x20code,\x20you:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20main
\x20package\x20(a\x20package\x20is\x20a\x20way\x20to\x20group\x0a\x20\x20\x20\x20\x20\x20\x20\x20functions,\x20and\x20it's\x20made\x20up\x20of\x20all\x20the\x20files\x20in\x20the\x20same\x20directory).\x0a\x20\x20\x20\x20\x20\x20fmt
\x20package,\x0a\x20\x20\x20\x20\x20\x20\x20\x20which\x20contains\x20functions\x20for\x20formatting\x20text,\x20including\x20printing\x20to\x20the\x0a\x20\x20\x20\x20\x20\x20\x20\x20console.\x20This\x20package\x20is\x20one\x20of\x20the\x0a\x20\x20\x20\x20\x20\x20\x20\x20standard\x20library\x20packages\x20you\x20got\x0a\x20\x20\x20\x20\x20\x20\x20\x20when\x20you\x20installed\x20Go.\x0a\x20\x20\x20\x20\x20\x20main
\x20function\x20to\x20print\x20a\x20message\x20to\x20the\x0a\x20\x20\x20\x20\x20\x20\x20\x20console.\x20A\x20main
\x20function\x20executes\x20by\x20default\x20when\x20you\x20run\x0a\x20\x20\x20\x20\x20\x20\x20\x20the\x20main
\x20package.\x0a\x20\x20\x20\x20\x20\x20\x0a$\x20go\x20run\x20.\x0aHello,\x20World!\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20The\x0a\x20\x20\x20\x20\x20\x20go\x20run
\x20command\x0a\x20\x20\x20\x20\x20\x20is\x20one\x20of\x20many\x20go
\x20commands\x20you'll\x20use\x20to\x20get\x20things\x20done\x20with\x0a\x20\x20\x20\x20\x20\x20Go.\x20Use\x20the\x20following\x20command\x20to\x20get\x20a\x20list\x20of\x20the\x20others:\x0a\x20\x20\x20\x20
\x0a$\x20go\x20help\x0a\x0a\x20\x20
\x0a\x20\x20When\x20you\x20need\x20your\x20code\x20to\x20do\x20something\x20that\x20might\x20have\x20been\x20implemented\x20by\x0a\x20\x20someone\x20else,\x20you\x20can\x20look\x20for\x20a\x20package\x20that\x20has\x20functions\x20you\x20can\x20use\x20in\x0a\x20\x20your\x20code.\x0a
\x0a\x0arsc.io/quote
\x20package\x20in\x20search\x20results\x0a\x20\x20\x20\x20\x20\x20\x20\x20(if\x20you\x20see\x20rsc.io/quote/v3
,\x20ignore\x20it\x20for\x20now).\x0a\x20\x20\x20\x20\x20\x20Go
\x20function.\x0a\x20\x20\x20\x20\x20\x20quote
\x20is\x0a\x20\x20\x20\x20\x20\x20\x20\x20included\x20in\x20the\x20rsc.io/quote
\x20module.\x0a\x20\x20\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20You\x20can\x20use\x20the\x20pkg.go.dev\x20site\x20to\x20find\x20published\x20modules\x20whose\x20packages\x0a\x20\x20\x20\x20\x20\x20have\x20functions\x20you\x20can\x20use\x20in\x20your\x20own\x20code.\x20Packages\x20are\x20published\x20in\x0a\x20\x20\x20\x20\x20\x20modules\x20--\x20like\x20rsc.io/quote
\x20--\x20where\x20others\x20can\x20use\x20them.\x0a\x20\x20\x20\x20\x20\x20Modules\x20are\x20improved\x20with\x20new\x20versions\x20over\x20time,\x20and\x20you\x20can\x20upgrade\x20your\x0a\x20\x20\x20\x20\x20\x20code\x20to\x20use\x20the\x20improved\x20versions.\x0a\x20\x20\x20\x20
rsc.io/quote
\x20package\x20and\x20add\x20a\x20call\x0a\x20\x20\x20\x20to\x20its\x20Go
\x20function.\x0a\x0a\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20After\x20adding\x20the\x20highlighted\x20lines,\x20your\x20code\x20should\x20include\x20the\x0a\x20\x20\x20\x20\x20\x20following:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0apackage\x20main\x0a\x0aimport\x20\"fmt\"\x0a\x0aimport\x20\"rsc.io/quote\"\x0a\x0afunc\x20main()\x20{\x0a\x20\x20\x20\x20fmt.Println(quote.Go())\x0a}\x0a\x0a\x20\x20
\x0a\x20\x20\x20\x20\x20\x20Go\x20will\x20add\x20the\x20quote
\x20module\x20as\x20a\x20requirement,\x20as\x20well\x20as\x20a\x20go.sum\x20file\x20for\x20use\x20in\x20authenticating\x20the\x20module.\x20For\x20more,\x20see\x20Module\x20authentication\x20using\x20go.sum.\x0a\x20\x20\x20\x20
\x0a$\x20go\x20mod\x20tidy\x0ago:\x20finding\x20module\x20for\x20package\x20rsc.io/quote\x0ago:\x20found\x20rsc.io/quote\x20in\x20rsc.io/quote\x20v1.5.2\x0a\x0a\x20\x20
\x0a$\x20go\x20run\x20.\x0aDon't\x20communicate\x20by\x20sharing\x20memory,\x20share\x20memory\x20by\x20communicating.\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20Notice\x20that\x20your\x20code\x20calls\x20the\x20Go
\x20function,\x20printing\x20a\x0a\x20\x20\x20\x20\x20\x20clever\x20message\x20about\x20communication.\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20When\x20you\x20ran\x20go\x20mod\x20tidy
,\x20it\x20located\x20and\x20downloaded\x20the\x0a\x20\x20\x20\x20\x20\x20rsc.io/quote
\x20module\x20that\x20contains\x20the\x20package\x20you\x20imported.\x0a\x20\x20\x20\x20\x20\x20By\x20default,\x20it\x20downloaded\x20the\x20latest\x20version\x20--\x20v1.5.2.\x0a\x20\x20\x20\x20
\x0a\x20\x20With\x20this\x20quick\x20introduction,\x20you\x20got\x20Go\x20installed\x20and\x20learned\x20some\x20of\x20the\x0a\x20\x20basics.\x20To\x20write\x20some\x20more\x20code\x20with\x20another\x20tutorial,\x20take\x20a\x20look\x20at\x0a\x20\x20Create\x20a\x20Go\x20module.\x0a
\x0a", "doc/tutorial/greetings-multiple-people.html": "\x0a\x0a\x0a\x20\x20In\x20the\x20last\x20changes\x20you'll\x20make\x20to\x20your\x20module's\x20code,\x20you'll\x20add\x20support\x20for\x0a\x20\x20getting\x20greetings\x20for\x20multiple\x20people\x20in\x20one\x20request.\x20In\x20other\x20words,\x20you'll\x0a\x20\x20handle\x20a\x20multiple-value\x20input\x20and\x20pair\x20values\x20with\x20a\x20multiple-value\x20output.\x0a
\x0a\x0a\x0a\x0a\x0a\x20\x20To\x20do\x20this,\x20you'll\x20need\x20to\x20pass\x20a\x20set\x20of\x20names\x20to\x20a\x20function\x20that\x20can\x20return\x20a\x0a\x20\x20greeting\x20for\x20each\x20of\x20them.\x20Changing\x20the\x20Hello
\x20function's\x0a\x20\x20parameter\x20from\x20a\x20single\x20name\x20to\x20a\x20set\x20of\x20names\x20would\x20change\x20the\x20function\x0a\x20\x20signature.\x20If\x20you\x20had\x20already\x20published\x20the\x20greetings
\x20module\x20and\x0a\x20\x20users\x20had\x20already\x20written\x20code\x20calling\x20Hello
,\x20that\x20change\x20would\x0a\x20\x20break\x20their\x20programs.\x20In\x20this\x20situation,\x20a\x20better\x20choice\x20is\x20to\x20give\x20new\x0a\x20\x20functionality\x20a\x20new\x20name.\x0a
\x0a\x20\x20In\x20the\x20last\x20code\x20you'll\x20add\x20with\x20this\x20tutorial,\x20update\x20the\x20code\x20as\x20if\x20you've\x0a\x20\x20already\x20published\x20a\x20version\x20of\x20the\x20greetings
\x20module.\x20Instead\x20of\x0a\x20\x20changing\x20the\x20Hello
\x20function,\x20add\x20a\x20new\x20function\x0a\x20\x20Hellos
\x20that\x20takes\x20a\x20set\x20of\x20names.\x20Then,\x20for\x20the\x20sake\x20of\x0a\x20\x20simplicity,\x20have\x20the\x20new\x20function\x20call\x20the\x20existing\x20one.\x20Keeping\x20both\x0a\x20\x20functions\x20in\x20the\x20package\x20leaves\x20the\x20original\x20for\x20existing\x20callers\x20(or\x20future\x0a\x20\x20callers\x20who\x20only\x20need\x20one\x20greeting)\x20and\x20adds\x20a\x20new\x20one\x20for\x20callers\x20that\x20want\x0a\x20\x20the\x20expanded\x20functionality.\x0a
\x0apackage\x20greetings\x0a\x0aimport\x20(\x0a\x20\x20\x20\x20\"errors\"\x0a\x20\x20\x20\x20\"fmt\"\x0a\x20\x20\x20\x20\"math/rand\"\x0a\x20\x20\x20\x20\"time\"\x0a)\x0a\x0a//\x20Hello\x20returns\x20a\x20greeting\x20for\x20the\x20named\x20person.\x0afunc\x20Hello(name\x20string)\x20(string,\x20error)\x20{\x0a\x20\x20\x20\x20//\x20If\x20no\x20name\x20was\x20given,\x20return\x20an\x20error\x20with\x20a\x20message.\x0a\x20\x20\x20\x20if\x20name\x20==\x20\"\"\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20return\x20name,\x20errors.New(\"empty\x20name\")\x0a\x20\x20\x20\x20}\x0a\x20\x20\x20\x20//\x20Create\x20a\x20message\x20using\x20a\x20random\x20format.\x0a\x20\x20\x20\x20message\x20:=\x20fmt.Sprintf(randomFormat(),\x20name)\x0a\x20\x20\x20\x20return\x20message,\x20nil\x0a}\x0a\x0a//\x20Hellos\x20returns\x20a\x20map\x20that\x20associates\x20each\x20of\x20the\x20named\x20people\x0a//\x20with\x20a\x20greeting\x20message.\x0afunc\x20Hellos(names\x20[]string)\x20(map[string]string,\x20error)\x20{\x0a\x20\x20\x20\x20//\x20A\x20map\x20to\x20associate\x20names\x20with\x20messages.\x0a\x20\x20\x20\x20messages\x20:=\x20make(map[string]string)\x0a\x20\x20\x20\x20//\x20Loop\x20through\x20the\x20received\x20slice\x20of\x20names,\x20calling\x0a\x20\x20\x20\x20//\x20the\x20Hello\x20function\x20to\x20get\x20a\x20message\x20for\x20each\x20name.\x0a\x20\x20\x20\x20for\x20_,\x20name\x20:=\x20range\x20names\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20message,\x20err\x20:=\x20Hello(name)\x0a\x20\x20\x20\x20\x20\x20\x20\x20if\x20err\x20!=\x20nil\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20return\x20nil,\x20err\x0a\x20\x20\x20\x20\x20\x20\x20\x20}\x0a\x20\x20\x20\x20\x20\x20\x20\x20//\x20In\x20the\x20map,\x20associate\x20the\x20retrieved\x20message\x20with\x20\x0a\x20\x20\x20\x20\x20\x20\x20\x20//\x20the\x20name.\x0a\x20\x20\x20\x20\x20\x20\x20\x20messages[name]\x20=\x20message\x0a\x20\x20\x20\x20}\x0a\x20\x20\x20\x20return\x20messages,\x20nil\x0a}\x0a\x0a//\x20Init\x20sets\x20initial\x20values\x20for\x20variables\x20used\x20in\x20the\x20function.\x0afunc\x20init()\x20{\x0a\x20\x20\x20\x20rand.Seed(time.Now().UnixNano())\x0a}\x0a\x0a//\x20randomFormat\x20returns\x20one\x20of\x20a\x20set\x20of\x20greeting\x20messages.\x20The\x20returned\x0a//\x20message\x20is\x20selected\x20at\x20random.\x0afunc\x20randomFormat()\x20string\x20{\x0a\x20\x20\x20\x20//\x20A\x20slice\x20of\x20message\x20formats.\x0a\x20\x20\x20\x20formats\x20:=\x20[]string{\x0a\x20\x20\x20\x20\x20\x20\x20\x20\"Hi,\x20%v.\x20Welcome!\",\x0a\x20\x20\x20\x20\x20\x20\x20\x20\"Great\x20to\x20see\x20you,\x20%v!\",\x0a\x20\x20\x20\x20\x20\x20\x20\x20\"Hail,\x20%v!\x20Well\x20met!\",\x0a\x20\x20\x20\x20}\x0a\x0a\x20\x20\x20\x20//\x20Return\x20one\x20of\x20the\x20message\x20formats\x20selected\x20at\x20random.\x0a\x20\x20\x20\x20return\x20formats[rand.Intn(len(formats))]\x0a}\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20In\x20this\x20code,\x20you:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20Hellos
\x20function\x20whose\x20parameter\x20is\x20a\x20slice\x20of\x20names\x0a\x20\x20\x20\x20\x20\x20\x20\x20rather\x20than\x20a\x20single\x20name.\x20Also,\x20you\x20change\x20one\x20of\x20its\x20return\x20types\x20from\x0a\x20\x20\x20\x20\x20\x20\x20\x20a\x20string
\x20to\x20a\x20map
\x20so\x20you\x20can\x20return\x20names\x0a\x20\x20\x20\x20\x20\x20\x20\x20mapped\x20to\x20greeting\x20messages.\x0a\x20\x20\x20\x20\x20\x20messages
\x0a\x20\x20\x20\x20\x20\x20\x20\x20map\x20to\x20associate\x20each\x20of\x20the\x0a\x20\x20\x20\x20\x20\x20\x20\x20received\x20names\x20(as\x20a\x20key)\x20with\x20a\x20generated\x20message\x20(as\x20a\x20value).\x20In\x20Go,\x0a\x20\x20\x20\x20\x20\x20\x20\x20you\x20initialize\x20a\x20map\x20with\x20the\x20following\x20syntax:\x0a\x20\x20\x20\x20\x20\x20\x20\x20make(map[key-type]value-type)
.\x20You\x20have\x0a\x20\x20\x20\x20\x20\x20\x20\x20the\x20Hellos
\x20function\x20return\x20this\x20map\x20to\x20the\x20caller.\x0a\x20\x20\x20\x20\x20\x20for
\x20loop,\x20range
\x20returns\x20two\x20values:\x20the\x20index\x0a\x20\x20\x20\x20\x20\x20\x20\x20of\x20the\x20current\x20item\x20in\x20the\x20loop\x20and\x20a\x20copy\x20of\x20the\x20item's\x20value.\x20You\x0a\x20\x20\x20\x20\x20\x20\x20\x20don't\x20need\x20the\x20index,\x20so\x20you\x20use\x20the\x20Go\x0a\x20\x20\x20\x20\x20\x20\x20\x20blank\x20identifier\x20(an\x20underscore)\x0a\x20\x20\x20\x20\x20\x20\x20\x20to\x20ignore\x20it.\x0a\x20\x20\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20In\x20hello.go,\x20change\x20your\x20code\x20so\x20it\x20looks\x20like\x20the\x20following.\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0apackage\x20main\x0a\x0aimport\x20(\x0a\x20\x20\x20\x20\"fmt\"\x0a\x20\x20\x20\x20\"log\"\x0a\x0a\x20\x20\x20\x20\"example.com/greetings\"\x0a)\x0a\x0afunc\x20main()\x20{\x0a\x20\x20\x20\x20//\x20Set\x20properties\x20of\x20the\x20predefined\x20Logger,\x20including\x0a\x20\x20\x20\x20//\x20the\x20log\x20entry\x20prefix\x20and\x20a\x20flag\x20to\x20disable\x20printing\x0a\x20\x20\x20\x20//\x20the\x20time,\x20source\x20file,\x20and\x20line\x20number.\x0a\x20\x20\x20\x20log.SetPrefix(\"greetings:\x20\")\x0a\x20\x20\x20\x20log.SetFlags(0)\x0a\x0a\x20\x20\x20\x20//\x20A\x20slice\x20of\x20names.\x0a\x20\x20\x20\x20names\x20:=\x20[]string{\"Gladys\",\x20\"Samantha\",\x20\"Darrin\"}\x0a\x0a\x20\x20\x20\x20//\x20Request\x20greeting\x20messages\x20for\x20the\x20names.\x0a\x20\x20\x20\x20messages,\x20err\x20:=\x20greetings.Hellos(names)\x0a\x20\x20\x20\x20if\x20err\x20!=\x20nil\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20log.Fatal(err)\x0a\x20\x20\x20\x20}\x0a\x20\x20\x20\x20//\x20If\x20no\x20error\x20was\x20returned,\x20print\x20the\x20returned\x20map\x20of\x0a\x20\x20\x20\x20//\x20messages\x20to\x20the\x20console.\x0a\x20\x20\x20\x20fmt.Println(messages)\x0a}\x0a\x0a\x0a\x20\x20\x20\x20
\x0a\x20\x20\x20\x20\x20\x20With\x20these\x20changes,\x20you:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20names
\x20variable\x20as\x20a\x20slice\x20type\x20holding\x20three\x0a\x20\x20\x20\x20\x20\x20\x20\x20names.\x0a\x20\x20\x20\x20\x20\x20names
\x20variable\x20as\x20the\x20argument\x20to\x20the\x0a\x20\x20\x20\x20\x20\x20\x20\x20Hellos
\x20function.\x0a\x20\x20\x20\x20\x20\x20\x0a\x20\x20\x20\x20\x20\x20The\x20output\x20should\x20be\x20a\x20string\x20representation\x20of\x20the\x20map\x20associating\x20names\x0a\x20\x20\x20\x20\x20\x20with\x20messages,\x20something\x20like\x20the\x20following:\x0a\x20\x20\x20\x20
\x0a\x0a\x20\x20\x20\x20\x0a$\x20go\x20run\x20hello.go\x0amap[Darrin:Hail,\x20Darrin!\x20Well\x20met!\x20Gladys:Hi,\x20Gladys.\x20Welcome!\x20Samantha:Hail,\x20Samantha!\x20Well\x20met!]\x0a\x0a\x20\x20
\x0a\x20\x20This\x20topic\x20introduced\x20maps\x20for\x20representing\x20name/value\x20pairs.\x20It\x20also\x0a\x20\x20introduced\x20the\x20idea\x20of\x0a\x20\x20preserving\x20backward\x20compatibility\x0a\x20\x20by\x20implementing\x20a\x20new\x20function\x20for\x20new\x20or\x20changed\x20functionality\x20in\x20a\x20module.\x0a\x20\x20In\x20the\x20tutorial's\x20next\x20topic,\x20you'll\x20use\x0a\x20\x20built-in\x20features\x20to\x20create\x20a\x20unit\x20test\x20for\x20your\x20code.\x0a
\x0a\x0a\x0a\x20\x20<\x20Return\x20a\x20random\x20greeting\x0a\x20\x20Add\x20a\x20test\x20>\x0a
\x0a",