在 Linux 系统中使用 Golang 编写并运行脚本,可以按照以下步骤进行:
sudo apt-get update
sudo apt-get install golang-go
~/.bashrc 或 ~/.bash_profile 文件,添加以下内容:export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
保存文件后,运行 source ~/.bashrc 或 source ~/.bash_profile 使配置生效。
hello.go。在文件中编写以下代码:package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
go run 命令运行脚本:cd /path/to/your/script
go run hello.go
这将输出 “Hello, World!”。
go build 命令:go build hello.go
这将在当前目录下生成一个名为 hello(在 Windows 上为 hello.exe)的可执行文件。你可以通过运行 ./hello(在 Windows 上为 hello.exe)来执行它。
#!/bin/bash
# 调用 Go 脚本
/path/to/your/script/hello
# 其他命令
确保为 Bash 脚本设置可执行权限:
chmod +x your_bash_script.sh
然后运行脚本:
./your_bash_script.sh
这将首先运行 Go 脚本,然后执行其他命令。