From 6ba27a496a089d0971c137a1a0632a9a10a3be22 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 12 Feb 2021 14:32:52 -0500 Subject: _content: move content/static/* to _content/* The extra level of hierarchy here is unnecessary and confusing. The directory is now _content so that any Go source files in our docs are not considered by commands like "go mod tidy" and "go test all". Change-Id: Ib6d7cb12920193798ee825155a8f8b33f16e60d8 Reviewed-on: https://go-review.googlesource.com/c/website/+/291691 Trust: Russ Cox Run-TryBot: Russ Cox TryBot-Result: Go Bot Reviewed-by: Dmitri Shuralyov --- _content/doc/tutorial/getting-started.html | 293 +++++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 _content/doc/tutorial/getting-started.html (limited to '_content/doc/tutorial/getting-started.html') diff --git a/_content/doc/tutorial/getting-started.html b/_content/doc/tutorial/getting-started.html new file mode 100644 index 00000000..a92cf209 --- /dev/null +++ b/_content/doc/tutorial/getting-started.html @@ -0,0 +1,293 @@ + + +

+ In this tutorial, you'll get a brief introduction to Go programming. Along the + way, you will: +

+ +
    +
  • Install Go (if you haven't already).
  • +
  • Write some simple "Hello, world" code.
  • +
  • Use the go command to run your code.
  • +
  • + Use the Go package discovery tool to find packages you can use in your own + code. +
  • +
  • Call functions of an external module.
  • +
+ + + +

Prerequisites

+ +
    +
  • + Some programming experience. The code here is pretty + simple, but it helps to know something about functions. +
  • +
  • + A tool to edit your code. 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). +
  • +
  • + A command terminal. Go works well using any terminal on + Linux and Mac, and on PowerShell or cmd in Windows. +
  • +
+ +

Install Go

+ +

Just use the Download and install steps.

+ +

Write some code

+ +

+ Get started with Hello, World. +

+ +
    +
  1. + Open a command prompt and cd to your home directory. + +

    + On Linux or Mac: +

    + +
    +cd
    +
    + +

    + On Windows: +

    + +
    +cd %HOMEPATH%
    +
    +
  2. + +
  3. + Create a hello directory for your first Go source code. + +

    + For example, use the following commands: +

    + +
    +mkdir hello
    +cd hello
    +
    +
  4. + +
  5. + Initialize a new module for tracking dependencies. + +

    + 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 + 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
    +
    +
  6. + +
  7. + In your text editor, create a file hello.go in which to write your code. +
  8. + +
  9. + Paste the following code into your hello.go file and save the file. + +
    +package main
    +
    +import "fmt"
    +
    +func main() {
    +    fmt.Println("Hello, World!")
    +}
    +
    + +

    + This is your Go code. In this code, you: +

    + +
      +
    • + Declare a main package (a package is a way to group + functions, and it's made up of all the files in the same directory). +
    • +
    • + Import the popular + fmt package, + which contains functions for formatting text, including printing to the + console. This package is one of the + standard library packages you got + when you installed Go. +
    • +
    • + Implement a main function to print a message to the + console. A main function executes by default when you run + the main package. +
    • +
    +
  10. + +
  11. + Run your code to see the greeting. + +
    +$ go run .
    +Hello, World!
    +
    + +

    + The + go run command + is one of many go commands you'll use to get things done with + Go. Use the following command to get a list of the others: +

    + +
    +$ go help
    +
    +
  12. +
+ +

Call code in an external package

+ +

+ When you need your code to do something that might have been implemented by + someone else, you can look for a package that has functions you can use in + your code. +

+ +
    +
  1. + Make your printed message a little more interesting with a function from an + external module. + +
      +
    1. + Visit pkg.go.dev and + search for a "quote" package. +
    2. +
    3. + Locate and click the rsc.io/quote package in search results + (if you see rsc.io/quote/v3, ignore it for now). +
    4. +
    5. + On the Doc tab, under Index, note the + list of functions you can call from your code. You'll use the + Go function. +
    6. +
    7. + At the top of this page, note that package quote is + included in the rsc.io/quote module. +
    8. +
    + +

    + You can use the pkg.go.dev site to find published modules whose packages + have functions you can use in your own code. Packages are published in + modules -- like rsc.io/quote -- where others can use them. + Modules are improved with new versions over time, and you can upgrade your + code to use the improved versions. +

    +
  2. + +
  3. + In your Go code, import the rsc.io/quote package and add a call + to its Go function. + +

    + After adding the highlighted lines, your code should include the + following: +

    + +
    +package main
    +
    +import "fmt"
    +
    +import "rsc.io/quote"
    +
    +func main() {
    +    fmt.Println(quote.Go())
    +}
    +
    +
  4. + +
  5. + Add new module requirements and sums. + +

    + Go will add the 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 tidy
    +go: finding module for package rsc.io/quote
    +go: found rsc.io/quote in rsc.io/quote v1.5.2
    +
    +
  6. + +
  7. + Run your code to see the message generated by the function you're calling. + +
    +$ go run .
    +Don't communicate by sharing memory, share memory by communicating.
    +
    + +

    + Notice that your code calls the Go function, printing a + clever message about communication. +

    + +

    + 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. +

    +
  8. +
+ +

Write more code

+ +

+ 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 + Create a Go module. +

-- cgit v1.3-6-g1900