文書更新:2020年11月17日(火) 午前8時21分49秒

Home > 備忘録 > 言語関連 > go に関すること > go のインストール( 37 )

必要なパッケージを install と環境設定

  1. golang をインストールする
  2. [root@server]# dnf install golang
  3. path に追加する
  4. /home/ユーザー名/.bashrc
    export GOPATH=/xxx/yyy/zzzz/go		// Go のワーキングディレクトリ
    export PATH=$PATH:$GOPATH/bin
  5. path を通す
  6. [root@server]# source /home/ユーザー名/.bashrc

Hello world

  1. source code
  2. $GOPATH/src/hello.go
    package main		// main パッケージであることを宣言
    
    import "fmt"		// fmt モジュールをインポート
    
    func main() {		// 最初に実行される main() 関数を定義
        fmt.Println("hello, world")
    }
  3. 実行 する
  4. ※方法1
    [root@server]# go run $GOPATH/src/hello.go	// 直接実行する
    
    ※方法2
    [root@server]# go build $GOPATH/src/hello.go	// コンパイルする
    [root@server]# $GOPATH/src/hello.go	// コンパイル後実行する

go の help

[root@server]# 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         add dependencies to current module and install them
	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.