갈루아의 반서재

앞서 설치한 GO 언어로 hello world 를 출력해보자. 

2020/11/16 - [프로그래밍 Better Programming/고 Go] - 우분투 18.04에 Go 1.15 설치하기 Install Go 1.15 on Ubuntu 18.04 LTS

 

작업디렉토리(/home/fossa/go)에 새파일 hello.go 생성한 후 다음의 코드를 입력한다.

package main

import "fmt"

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

 

위의 코드를 하나씩 살펴보자.

  • 메인 패키지를 선언한다(패키지는 함수를 모으는 일종의 방법이다).
  • fmt package 를 임포트한다. 해당 패키지는 콘솔 출력 및 텍스트 포맷팅 관련 함수를 포함하고 있으며, Go 를 설치할 때 생성되는 standard library 의 하나다.
  • 콘솔에 메시지를 출력할 수 있는 메인 함수를 구현한다. 메인 함수의 경우 기본적으로 이 파일의 코드를 실행시킬 때 작동하는 함수이다.

다음과 같이 위의 코드를 실행시킨다.

(quintic) fossa@fossa:~$ cd go
(quintic) fossa@fossa:~/go$ go run hello.go
Hello, World!

go 명령어 관련 도움말은 다음에서 확인할 수 있다.

(quintic) fossa@fossa:~/go$ go help
Go is a tool for managing Go source code.

Usage:

        go <command> [arguments]

The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         download and install packages and dependencies
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

        buildconstraint build constraints
        buildmode       build modes
        c               calling between Go and C
        cache           build and test caching
        environment     environment variables
        filetype        file types
        go.mod          the go.mod file
        gopath          GOPATH environment variable
        gopath-get      legacy GOPATH go get
        goproxy         module proxy protocol
        importpath      import path syntax
        modules         modules, module versions, and more
        module-get      module-aware go get
        module-auth     module authentication using go.sum
        module-private  module configuration for non-public modules
        packages        package lists and patterns
        testflag        testing flags
        testfunc        testing functions

Use "go help <topic>" for more information about that topic.

(quintic) fossa@fossa:~/go$

golang.org/doc/tutorial/getting-started

 

Tutorial: Get started with Go - The Go Programming Language

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

golang.org