编写Linux下第一个Go程序(2024版)

编写Linux下第一个Go程序(2024版)

安装

首先是安装GO语言包,预备要安装的是go version go1.22.4 linux/amd64,

移步至https://golang.google.cn/dl/ ,选择go1.22.4.linux-amd64.tar.gz 下载

  1. Remove any previous Go installation by deleting the /usr/local/go folder (if it exists), then extract the archive you just downloaded into /usr/local, creating a fresh Go tree in /usr/local/go:
sh 复制代码
 rm -rf /usr/local/go && tar -C /usr/local -xzf go1.22.4.linux-amd64.tar.gz

(You may need to run the command as root or through sudo).

Do not untar the archive into an existing /usr/local/go tree. This is known to produce broken Go installations.

  1. Add /usr/local/go/bin to the PATH environment variable.
    You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation):
sh 复制代码
export PATH=$PATH:/usr/local/go/bin

Note: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile.

  1. Verify that you've installed Go by opening a command prompt and typing the following command:
sh 复制代码
$ go version

Confirm that the command prints the installed version of Go.

编写HelloWorld程序

  1. 建立hello目录
sh 复制代码
mkdir hello
cd hello
  1. 使能依赖跟踪 Enable dependency tracking for your code
sh 复制代码
$ go mod init example/hello
go: creating new go.mod: module example/hello

在实际开发中,模块路径通常是存储库 源代码的保存位置。例如,模块路径可能是 。如果您计划发布 您的模块供其他人使用,模块路径必须是 Go 工具可以从中下载模块的位置。有关的更多信息 使用模块路径命名模块,请参阅管理依赖项

  1. 编写hello.go
go 复制代码
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
  1. 运行
sh 复制代码
$ go run .
Hello, World!

go run 命令是用于完成工作的众多命令之一。使用以下命令获取其他命令的列表:

复制代码
$ go help
  1. 调用外部包中的代码
    访问 pkg.go.dev 并搜索 "quote" 包.忽略版本号 rsc.io/quotersc.io/quote/v3
    包名为:rsc.io/quote

You can use the pkg.go.dev site to find published modules whose packages have functions you can use in your own code. Packages are published in modules -- like -- where others can use them. Modules are improved with new versions over time, and you can upgrade your code to use the improved versions. rsc.io/quote

  1. In your Go code, import the package and add a call to its function. rsc.io/quoteGo
    After adding the highlighted lines, your code should include the following:
go 复制代码
package main

import "fmt"

import "rsc.io/quote"

func main() {
    fmt.Println(quote.Go())
}
  1. 添加新模块的requirements and sums.
sh 复制代码
cps@linx:~/hello$ go mod tidy
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2
  1. 运行新模块
go 复制代码
$ go run .

Don't communicate by sharing memory, share memory by communicating.

相关推荐
晓13138 分钟前
第七章 【C语言篇:文件】 文件全面解析
linux·c语言·开发语言
唐装鼠17 分钟前
Linux 下 malloc 内存分配机制详解
linux·malloc
予枫的编程笔记17 分钟前
【Linux入门篇】Linux运维必学:Vim核心操作详解,告别编辑器依赖
linux·人工智能·linux运维·vim操作教程·程序员工具·编辑器技巧·新手学vim
17(无规则自律)30 分钟前
深入浅出 Linux 内核模块,写一个内核版的 Hello World
linux·arm开发·嵌入式硬件
liu****43 分钟前
2.深入浅出理解虚拟化与容器化(含Docker实操全解析)
运维·c++·docker·容器·虚拟化技术
中二病码农不会遇见C++学姐1 小时前
Linux下的.run文件
linux
予枫的编程笔记1 小时前
【Linux入门篇】摆脱权限混乱困境:Linux用户组管理+sudo提权,一步到位
linux·linux运维·后端开发·linux用户管理·linux权限配置·chmod命令·sudo配置
一个人旅程~1 小时前
Dell n4020双系统分区步骤和linux优化操作
linux·windows·电脑
池央1 小时前
CANN 诊断工具链深度解析:oam-tools 的自动化故障信息收集、软硬件状态快照与 AI Core 错误溯源机制
运维·人工智能·自动化
忆~遂愿1 小时前
CANN metadef 深度解析:动态形状元数据管理、图编译器接口规范与序列化执行机制
大数据·linux