• 검색 결과가 없습니다.

func main() {

Step 4 — Installing Go

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.

Step 5 — Creating Your 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:

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.

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.

The src subdirectory may contain multiple version control repositories (such as Git, Mercurial, and Bazaar). You will see directories like github.com or golang.org when your program imports third party libraries. If you are using a code repository like github.com, you will also put your projects and source files under that directory. This allows for a canonical import of code in your project. Canonical imports are imports that reference a fully qualified package, such as github.com/digitalocean/godo.

Here is what a typical workspace may look like:

.

├── bin

│ ├── buffalo

# command executable

│ ├── dlv

# command executable

│ └── packr

# command executable

└── src

└── github.com

└── digitalocean └── godo

├── .git

# Git repository metadata

├── account.go

# package source

├── account_test.go

# test source

├── ...

├── timestamp.go

├── timestamp_test.go └── util

├── droplet.go

└── droplet_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

Issue 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

Prior to Go 1.8, it was required to set a local environment variable called $GOPATH. While it is no longer explicitly required to do so, it is still considered a good practice as many third party tools still depend on this variable being set.

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 sammy:

Output

C:\Users\sammy\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

If you were working on the https://github.com/digitalocean/godo project, you would put it in the following directory:

$GOPATH/src/github.com/digitalocean/godo

Structuring your projects in this manner will make projects available with the go get tool. It will also help readability later.

You can verify this by using the go get command to fetch the godo library:

go get github.com/digitalocean/godo

Note: If you don’t have git installed, Windows will open a dialog box asking if you want to install it. Click Yes to continue and follow the installation instructions.

You can see it successfully downloaded the godo package by listing the directory:

ls $env:GOPATH/src/github.com/digitalocean/godo

You will receive output similar to this:

Output

Directory:

C:\Users\sammy\go\src\github.com\digitalocean\godo

Mode LastWriteTime Length Name

----

d--- 4/10/2019 2:59 PM util

-a---- 4/10/2019 2:59 PM 9 .gitignore

-a---- 4/10/2019 2:59 PM 69 .travis.yml

-a---- 4/10/2019 2:59 PM 1592 account.go

-a---- 4/10/2019 2:59 PM 1679 account_test.go

-rw-r--r-- 1 sammy staff 2892 Apr 5 15:56 CHANGELOG.md

-rw-r--r-- 1 sammy staff 1851 Apr 5 15:56 CONTRIBUTING.md

. . .

-a---- 4/10/2019 2:59 PM 5076

vpcs.go

-a---- 4/10/2019 2:59 PM 4309 vpcs_test.go

In this step, you created a Go workspace and configured the necessary environment variables. In the next step you will test the workspace with some code.

Step 6 — Creating a Simple Program

Now that you have the Go workspace set up, create a simple “Hello, World!” program. This will make sure that your workspace is configured properly, and also gives you the opportunity to become more familiar with Go. Because you are creating a single Go source file, and not an actual project, you don’t need to be in your workspace to do this.

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

nano hello.go

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

hello.go

Exit nano by pressing the CTRL and X keys. When prompted to save the file, press Y and then ENTER.

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.

Once you exit out of nano and return to your shell, run the program:

go run hello.go

The hello.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.

Conclusion

package main import "fmt"

func main() {

fmt.Println("Hello, World!")

}

Congratulations! At this point you have a Go programming workspace set

up on your local Windows machine and can begin a coding project!

How To Write Your First Program in Go

Written by Gopher Guides

The “Hello, World!” program is a classic and time-honored tradition in computer programming. It’s a simple and complete first program for beginners, and it’s a good way to make sure your environment is properly configured.

This tutorial will walk you through creating this program in Go.

However, to make the program more interesting, you’ll modify the traditional “Hello, World!” program so that it asks the user for their name.

You’ll then use the name in the greeting. When you’re done with the tutorial, you’ll have a program that looks like this when you run it:

Output

Please enter your name.

관련 문서