Movatterモバイル変換


[0]ホーム

URL:


  1. Documentation
  2. Tutorials
  3. Tutorial: Get started with Go

Tutorial: Get started with Go

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

Note: For other tutorials, seeTutorials.

Prerequisites

Install Go

Just use theDownload 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. Create a hello directory for your first Go source code.

    For example, use the following commands:

    mkdir hellocd hello
  3. Enable dependency tracking for your code.

    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.

    To enable dependency tracking for your code by creating a go.mod file, run thego mod init command, giving it the name of the module your code will be in. The name is the module's module path.

    In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might begithub.com/mymodule. If you plan to publish your module for others to use, the module pathmust be a location from which Go tools can download your module. For more about naming a module with a module path, seeManaging dependencies.

    For the purposes of this tutorial, just useexample/hello.

    $ go mod init example/hellogo: creating new go.mod: module example/hello
  4. In your text editor, create a file hello.go in which to write your code.

  5. Paste the following code into your hello.go file and save the file.

    package mainimport "fmt"func main() {    fmt.Println("Hello, World!")}

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

    • Declare amain package (a package is a way to group functions, and it's made up of all the files in the same directory).
    • Import the popularfmt package, which contains functions for formatting text, including printing to the console. This package is one of thestandard library packages you got when you installed Go.
    • Implement amain function to print a message to the console. Amain function executes by default when you run themain package.
  6. Run your code to see the greeting.

    $ go run .Hello, World!

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

    $ go help

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 andsearch for a "quote" package.
    2. In the search results, locate and click on the v1 of thersc.io/quote package (it should be listed with the "Other major versions" ofrsc.io/quote/v4).
    3. In theDocumentation section, underIndex, note the list of functions you can call from your code. You'll use theGo function.
    4. At the top of this page, note that packagequote is included in thersc.io/quote module.

    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 -- likersc.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. In your Go code, import thersc.io/quote package and add a call to itsGo function.

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

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

    Go will add thequote module as a requirement, as well as a go.sum file for use in authenticating the module. For more, seeAuthenticating modules in the Go Modules Reference.

    $ go mod tidygo: finding module for package rsc.io/quotego: found rsc.io/quote in rsc.io/quote v1.5.2
  4. 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 theGo function, printing a clever message about communication.

    When you rango mod tidy, it located and downloaded thersc.io/quote module that contains the package you imported. By default, it downloaded the latest version -- v1.5.2.

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

go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp