How to install golang on linux

Download and install

Download and install Go quickly with the steps described here.

For other content on installing, you might be interested in:

  • Managing Go installations — How to install multiple versions and uninstall.
  • Installing Go from source — How to check out the sources, build them on your own machine, and run them.

Don’t see your operating system here? Try one of the other downloads.

Go installation

Select the tab for your computer’s operating system below, then follow its installation instructions.

  1. Remove any previous Go installation by deleting the /usr/local/go folder (if it exists), then extract the archive you just downloaded into /usr/local, creating a fresh Go tree in /usr/local/go:
$ rm -rf /usr/local/go && tar -C /usr/local -xzf go1.14.3.linux-amd64.tar.gz  
export PATH=$PATH:/usr/local/go/bin  
$ go version  
  1. Open the package file you downloaded and follow the prompts to install Go. The package installs the Go distribution to /usr/local/go. The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.
  2. Verify that you’ve installed Go by opening a command prompt and typing the following command:
$ go version  
  1. Open the MSI file you downloaded and follow the prompts to install Go. By default, the installer will install Go to Program Files or Program Files (x86) . You can change the location as needed. After installing, you will need to close and reopen any open command prompts so that changes to the environment made by the installer are reflected at the command prompt.
  2. Verify that you’ve installed Go.
    1. In Windows, click the Start menu.
    2. In the menu’s search box, type cmd , then press the Enter key.
    3. In the Command Prompt window that appears, type the following command:
    $ go version  

    You’re all set!

    Visit the Getting Started tutorial to write some simple Go code. It takes about 10 minutes to complete.

    Report Issues

    If you spot bugs, mistakes, or inconsistencies in the Go project’s code or documentation, please let us know by filing a ticket on our issue tracker. Of course, you should check it’s not an existing issue before creating a new one.

    Источник

    How To Install Go on Ubuntu 20.04

    Go, sometimes referred to as “Golang”, is an open-source programming language that was released by Google in 2012. Google’s intention was to create a programming language that could be learned quickly.

    Since its release, Go has become highly popular among developers and is used for various applications ranging from cloud or server-side applications, to artificial intelligence and robotics. This tutorial outlines how to download and install the latest version of Go (currently version 1.16.7 ) on an Ubuntu 20.04 server, build the famous Hello, World! application, and make your Go code into an executable binary for future use.

    Prerequisites

    This tutorial requires an Ubuntu 20.04 system configured with a non-root user with sudo privileges and a firewall as described in Initial Server Setup with Ubuntu 20.04.

    Step 1 — Installing Go

    In this step, you will install Go on your server.

    First, connect to your Ubuntu server via ssh :

    Next, navigate to the official Go downloads page in your web browser. From there, copy the URL for the current binary release’s tarball.

    As of this writing, the latest release is go1.16.7 . To install Go on an Ubuntu server (or any Linux server, for that matter), copy the URL of the file ending with linux-amd64.tar.gz .

    Now that you have your link ready, first confirm that you’re in the home directory:

    Then use curl to retrieve the tarball, making sure to replace the highlighted URL with the one you just copied. The -O flag ensures that this outputs to a file, and the L flag instructs HTTPS redirects, since this link was taken from the Go website and will redirect here before the file downloads:

    To verify the integrity of the file you downloaded, run the sha256sum command and pass it to the filename as an argument:

    This will return the tarball’s SHA256 checksum:

    Output
    go1.16.7.linux-amd64.tar.gz 7fe7a73f55ba3e2285da36f8b085e5c0159e9564ef5f63ee0ed6b818ade8ef04 go1.16.7.linux-amd64.tar.gz

    If the checksum matches the one listed on the downloads page, you’ve done this step correctly.

    Next, use tar to extract the tarball. This command includes the -C flag which instructs tar to change to the given directory before performing any other operations. This means that the extracted files will be written to the /usr/local/ directory, the recommended location for installing Go… The x flag tells tar to extract, v tells it we want verbose output (a listing of the files being extracted), and f tells it we’ll specify a filename:

    Although /usr/local/go is the recommended location for installing Go, some users may prefer or require different paths.

    Step 2 — Setting Go Paths

    In this step, you will set paths in your environment.

    First, set Go’s root value, which tells Go where to look for its files. You can do this by editing the .profile file, which contains a list of commands that the system runs every time you log in.

    Use your preferred editor to open .profile , which is stored in your user’s home directory. Here, we’ll use nano :

    Then, add the following information to the end of your file:

    . . . export PATH=$PATH:/usr/local/go/bin 

    After you’ve added this information to your profile, save and close the file. If you used nano , do so by pressing CTRL+X , then Y , and then ENTER .

    Next, refresh your profile by running the following command:

    After, check if you can execute go commands by running go version :

    This command will output the release number of whatever version of Go is installed on your system:

    Output
    go version go1.16.7 linux/amd64

    This output confirms that you are now running Go on your server.

    Step 3 — Testing Your Install

    Now that Go is installed and the paths are set for your server, you can try creating your Hello, World! application to ensure that Go is working.

    First, create a new directory for your Go workspace, which is where Go will build its files:

    Then move into the directory you just created:

    When importing packages, you have to manage the dependencies through the code’s own module. You can do this by creating a go.mod file with the go mod init command:

    Next, create a Hello, World! Go file in your preferred text editor:

    Add the following text into your hello.go file:

    package main import "fmt" func main()

    Then, save and close the file by pressing CTRL+X , then Y , and then ENTER .

    Test your code to check that it prints the Hello, World! greeting:

    The go run command compiles and runs the Go package from a list of .go source files from the new hello directory you created and the path you imported. But, you can also use go build to make an executable file that can save you some time.

    Step 4 — Turning Your Go Code Into a Binary Executable

    The go run command is typically used as a shortcut for compiling and running a program that requires frequent changes. In cases where you’ve finished your code and want to run it without compiling it each time, you can use go build to turn your code into an executable binary. Building your code into an executable binary consolidates your application into a single file with all the support code necessary to execute the binary. Once you’ve built the binary executable, you can then run go install to place your program on an executable file path so you can run it from anywhere on your system. Then, your program will successfully print Hello, World! when prompted and you won’t need to compile the program again.

    Try it out and run go build . Make sure you run this from the same directory where your hello.go file is stored:

    Next, run ./hello to confirm the code is working properly:

    This confirms that you’ve successfully turned your hello.go code into an executable binary. However, you can only invoke this binary from within this directory. If you wanted to run this program from a different location on your server, you would need to specify the binary’s full file path in order to execute it.

    Typing out a binary’s full file path can quickly become tedious. As an alternative, you can run the go install command. This is similar to go build but instead of leaving the executable in the current directory, go install places it in the $GOPATH/bin directory, which will allow you to run it from any location on your server.

    In order to run go install successfully, you must pass it the install path of the binary you created with go build . To find the binary’s install path, run the following go list command:

    go list generates a list of any named Go packages stored in the current working directory. The f flag will cause go list to return output in a different format based on whatever package template you pass to it. This command tells it to use the Target template, which will cause go list to return the install path of any packages stored in this directory:

    Output
    ‘/home/sammy/go/bin/hello

    This is the install path of the binary file you created with go build . This means that the directory where this binary is installed is /home/ sammy /go/bin/ .

    Add this install directory to your system’s shell path. Be sure to change the highlighted portion of this command to reflect the install directory of the binary on your system, if different:

    Finally, run go install to compile and install the package:

    Try running this executable binary by just entering hello

    If you received the Hello, World! output, you’ve successfully made your Go program executable from both a specific and unspecified path on your server.

    Conclusion

    By downloading and installing the latest Go package and setting its paths, you now have a system to use for Go development. You can find and subscribe to additional articles on installing and using Go within our “Go” tag

    Get Ubuntu on a hosted virtual machine in seconds with DigitalOcean Droplets! Simple enough for any user, powerful enough for fast-growing applications or businesses.

    Источник

    Tutorial: Get started with Go

    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

    Write some code

    Get started with Hello, World.

      Open a command prompt and cd to your home directory. On Linux or Mac:

    • 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.
    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. Locate and click the rsc.io/quote package in search results (if you see rsc.io/quote/v3, ignore it for now).
      3. In the Documentation section, under Index, note the list of functions you can call from your code. You’ll use the Go function.
      4. At the top of this page, note that package quote is included in the rsc.io/quote module.

      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.

      Источник

      Читайте также:  Linux mint and steam
Оцените статью
Adblock
detector