Quantcast
Channel: Tech Support
Viewing all articles
Browse latest Browse all 880

How To Set Up Local Programming Environment Using Go on Windows 10

$
0
0

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. In this article we will guide you through installing Go on your local Windows 10 machine and setting up a programming environment using the powershell.

Prerequisites
To begin this tutorial, you will need a Windows 10 machine with administrative access and must have internet connectivity.

Configure PowerShell
To open Windows PowerShell, you can right-click on the Start menu icon on the lower left-hand corner of your screen. When the menu pops up, click on Search, and then type PowerShell into the search bar. When you are presented with options, right-click on Windows PowerShell from the Desktop app. For the sake of this guide, select Run as Administrator. When you are prompted with a dialog box that asks "Do you want to allow this app to make changes to your PC?" click on Yes.

Once you do this, you’ll see a text-based interface that has a string of words that looks like this:


Switch to system folder by typing the following command:

cd ~

You'll then be in a home directory such as PS C:\Users\yourusername

To continue with the installation process, you must first set up permissions through PowerShell. Configured to run in the most secure mode by default, there are a few levels of permissions that you can set up as an administrator:

Restricted is the default execution policy. Under this mode you will not be able to run scripts, and PowerShell will work only as an interactive shell.

AllSigned will enable you to run all scripts and configuration files that are signed by a trusted publisher, meaning that you could potentially open your machine up to the risk of running malicious scripts that happen to be signed by a trusted publisher.

RemoteSigned will let you run scripts and configuration files downloaded from the internet signed by trusted publishers, again opening your machine up to vulnerabilities if these trusted scripts are actually malicious.

Unrestricted will run all scripts and configuration files downloaded from the internet as soon as you confirm that you understand that the file was downloaded from the internet. In this case no digital signature is required, so you could be opening your machine up to the risk of running unsigned and potentially malicious scripts downloaded from the internet.

For the sake of this guide, we will use the RemoteSigned execution policy to set the permissions for the current user. This will allow the PowerShell to accept trusted scripts without making the permissions as broad as they would be with an Unrestricted permission.

Type the following command in PowerShell:

Set-ExecutionPolicy -Scope CurrentUser

PowerShell will then prompt you to provide an execution policy. Type the following to use RemoteSigned:

RemoteSigned

Once you press ENTER, you’ll be asked to confirm the change to the execution policy. Type the letter y to allow the changes to take effect. You can confirm that this worked by asking for the current permissions across the machine:

Get-ExecutionPolicy -List

You should receive output that looks something like this:

Output:
        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine       Undefined

This confirms that the current user can run trusted scripts downloaded from the internet. You can now move on to downloading the files we will need to set up our Go programming environment.

Install Package Manager Chocolatey
A package manager is a collection of software tools that work to automate installation processes. Start by creating a WebClient object called $script that shares internet connection settings with Internet Explorer:

$script = New-Object Net.WebClient

Take a look at the available options by piping the $script object with | to the Get-Member class:

$script | Get-Member

This will return all members (properties and methods) of this WebClient object:

Output
DownloadFileAsync         Method     void DownloadFileAsync(uri address, string fileName), void DownloadFileAsync(ur...
DownloadFileTaskAsync     Method     System.Threading.Tasks.Task DownloadFileTaskAsync(string address, string fileNa...
DownloadString            Method     string DownloadString(string address), string DownloadString(uri address) #method we will use
DownloadStringAsync       Method     void DownloadStringAsync(uri address), void DownloadStringAsync(uri address, Sy...
DownloadStringTaskAsync   Method     System.Threading.Tasks.Task[string] DownloadStringTaskAsync(string address), Sy…

Looking over the output, you can identify the DownloadString method used to display the script and signature in the PowerShell window. Use this method to inspect the script:

$script.DownloadString("https://chocolatey.org/install.ps1")

After inspecting the script, install Chocolatey by typing the following into PowerShell:

iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

Allow PowerShell to install Chocolatey. Once it is fully installed, you can begin installing additional tools with the choco command.

If you need to upgrade Chocolatey at any time in the future, run the following command:

choco upgrade chocolatey

With the package manager installed, you can install the rest of what you need for the Go programming environment.

Install Go
We will use Chocolatey package manager to install Go on Windows 10 machine:

choco install -y golang

PowerShell will now install Go, generating output within PowerShell during that process. Once the install is completed, you should see the following output:

Output
Environment Vars (like PATH) have changed. Close/reopen your shell to
see the changes (or in powershell/cmd.exe just type `refreshenv`).
The install of golang was successful.
 Software installed as 'msi', install location is likely default.

Chocolatey installed 1/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

With the installation finished, you’ll now confirm that Go is installed. To see the changes, close and re-open PowerShell as an Administrator, then check the version of Go available on your local machine:

go version

You'll receive output similar to the following:

Output
go version go1.12.1 windows/amd643.7.0

Once Go is installed, you can set up a workspace for your development projects.

Create Go Workspace
Now that you have Chocolatey, nano, and Go installed, you can create your programming workspace.

The Go workspace will contain two directories at its root:

1. src - The directory that contains Go source files. A source file is a file that you write using the Go programming language. Source files are used by the Go compiler to create an executable binary file.

2. bin - The directory that contains executables built and installed by the Go tools. Executables are binary files that run on your system and execute tasks. These are typically the programs compiled by your source code or another downloaded Go source code.

Here is what a typical workspace may look like:

.
├── bin
│   ├── buffalo                                      # command executable
│   ├── dlv                                          # command executable
│   └── packr                                        # command executable
└── src
    └── github.com
        └── techsupportpk
            └── godo
                ├── .git                            # Git repository metadata
                ├── account.go                      # package source
                ├── account_test.go                 # test source
                ├── ...
                ├── timestamp.go
                ├── timestamp_test.go
                └── util
                    ├── vm.go
                    └── vm_test.go

The default directory for the Go workspace as of 1.8 is your user's home directory with a go subdirectory, or $HOME/go. If you are using an earlier version of Go than 1.8, it is still considered best practice to use the $HOME/go location for your workspace

Type the following command to navigate to the $HOME directory:

cd $HOME

Next, create the directory structure for your Go workspace:

mkdir go/bin, go/src

This will ensure the following directory structure is now in place:

└── $HOME
    └── go
        ├── bin
        └── src

Since you used Chocolatey for the installation, this environment variable should already be set. You can verify this with the following command:

$env:GOPATH

You should see the following output, with your username in place of username:

Output
C:\Users\username\go

When Go compiles and installs tools, it will put them in the $GOPATH/bin directory. For convenience, it's common to add the workspace's bin subdirectory to your $PATH. You can do this using the setx command in PowerShell:

setx PATH "$($env:path);$GOPATH\bin"

This will now allow you to run any programs you compile or download via the Go tools anywhere on your system.

Now that you have the root of the workspace created and your $GOPATH environment variable set, you will create your future projects with the following directory structure. This example assumes you are using github.com as your repository:

$GOPATH/src/github.com/username/project

Build a Simple Program
From your home directory, open up a command-line text editor, such as notepad, and create a new file:

notepad test.go

Once the text file opens up in notepad, type out your program:

package main

import "fmt"

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

Exit notepad and when prompted to save the file, save it.

This code will use the fmt package and call the Println function with Hello, World! as the argument. This will cause the phrase Hello, World! to print out to the terminal when the program is run.

Return to your powershell, run the program:

go run test.go

The test.go program that you just created should cause PowerShell to produce the following output:

Output
Hello, World!

In this step, you used a basic program to verify that your Go workspace is properly configured.

Wrapping up
At this point you have a Go programming workspace set up on your local Windows 10 machine and now you can begin coding projects.

Viewing all articles
Browse latest Browse all 880

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>