10分钟上手 Go 语言:从安装到编程实战!

10分钟上手 Go 语言:从安装到编程实战!

在如今的编程世界中,Go 语言因其高效、简洁和易学的特点,成为了越来越多开发者的首选语言。无论是构建高性能的后端系统,还是处理并发任务,Go 都能轻松应对。对于刚刚接触 Go 的开发者来说,如何快速上手,掌握基础并开始实际编程,往往是最关心的问题。本篇文章将带你在短短10分钟内,从安装环境到编写第一个Go程序,实现真正的“快速上手”。

本文为初学者提供了一条简洁有效的学习路径,帮助你在10分钟内完成 Go 语言的安装和配置,并通过实际编程任务,快速掌握 Go 的基础语法、常用功能和实用技巧。文章包含了如何使用 Go 编写简单的程序、进行基本的算术运算、格式化输出、处理变量和常量等内容,配合实际代码示例,帮助读者更好地理解和应用这些概念。无论你是刚接触编程,还是想了解 Go 语言的开发者,都可以通过本篇文章快速上手。

一、简介和准备

什么是 Go?

  • Go 语言是一门编译语言。
  • 在你运行程序之前,Go首先使用编译器把你的代码转换成机器能够读懂的 1 和 0。
  • 它会把你所有的代码编译成一个可执行文件,在编译的过程中,Go编译器能够捕获一些错误。
  • 不是所有的编程语言都使用这种方式。
  • Python、Ruby 等很多语言都是使用解释器,随着程序的运行,一个语句一个语句的进行翻译。但这也意味着 bug 可能就潜伏在你还没有测试过的路径上。
  • 这些就是解释型语言。

Go 的由来和评价

“我们想要一个安全的、静态编译的、高性能的、类似 C++ 和 Java 这样的语言,但是得更轻量级并且要像Python这种动态解释型语言这样有趣。” ---- Rob Pike

Go语言是通过对软件开发体验这方面经过大量深思熟虑之后而设计的。大量的程序只需一个命令便在几秒钟内就能完成编译。该语言省略了能导致歧义的特性,鼓励了可预测且易于理解的代码。Go为Java等经典语言所强加的刚性结构,提供了替代品。

Go是一个开源的编程语言,能够构建大规模简单、高效、可靠的软件。

Go Playground

安装 Go 和开发工具

官网:https://go.dev/dl/

Go语言中文网:https://studygolang.com/

包和函数 package & function

package main

import (
  "fmt"
)

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

作业题

  • Go 编译器有哪些优点?
  • Go 的程序从哪里开始运行?
  • fmt 这个 package 提供了哪些功能?
  • 左花括号 { 放在哪不会引起语法错误?
  • 编写一个程序:
  • 第一行输出 Hello,World
  • 第二行输出 Hello。
package main

import "fmt"

// The function declares and initializes variables of different data types in Go.
func main() {
 fmt.Println("Hello World!")
 fmt.Println("Hello")
}

二、做个计算器

算术运算符

  • Go 语言提供了 +、-、*、/、% 来做加减乘除和取余的算术运算
// My weight loss program.
package main

import "fmt"

// main is the function where it all begins.
func main() {
 fmt.Print("My weight on the surface of Mars is")
 fmt.Print(149.0 * 0.3783)
 fmt.Print(" libs, and I would be ")
 fmt.Print(41 * 365 / 687)
 fmt.Print(" years old.")

 fmt.Println("My weight on the surface of Mars is", 149.0*0.3783, "libs, and I would be", 41*365.2425/687, "years old.")
}

运行

Code/go/hello via 🐹 v1.20.3 
➜ go run main.go
My weight on the surface of Mars is56.3667 libs, and I would be 21 years old.My weight on the surface of Mars is 56.3667 libs, and I would be 21.79758733624454 years old.

Code/go/hello via 🐹 v1.20.3 
➜ 

fmt.Print和fmt.Println

  • 上述例子里使用了Print,Println函数。
  • 可以传递若干个参数,之间用逗号分开。
  • 参数可以是字符串、数字、数学表达式等等。

格式化打印

  • 可以使用Printf来控制打印的输出结果。
  • 与Print和Println不同,Printf的第一个参数必须是字符串。
  • 这个字符串里包含了像%v这样的格式化动词,它的值由第二个参数的值所代替。
  • 如果指定了多个格式化动词,那么它们的值由后边的参数值按其顺序进行替换。
// My weight loss program.
package main

import "fmt"

// main is the function where it all begins.
func main() {
 fmt.Printf("My weight on the surface of Mars is %v libs,", 149.0*0.3783)
 fmt.Printf(" and I would be %v years old.\n", 41*365/687)

 fmt.Printf("My weight on the surface of %v is %v lbs.\n", "Earth", 149.0)
}

运行

Code/go/hello via 🐹 v1.20.3 
➜ go run main.go
My weight on the surface of Mars is 56.3667 libs, and I would be 21 years old.
My weight on the surface of Earth is 149 lbs.

Code/go/hello via 🐹 v1.20.3 
➜ 

使用Printf对齐文本

  • 在格式化动词里指定宽度,就可以对齐文本。
  • 例如,%4v,就是向左填充到足够4个宽度
  • 正数,向左填充空格
  • 负数,向右填充空格
// My weight loss program.
package main

import "fmt"

// main is the function where it all begins.
func main() {
 fmt.Printf("%-15v $%4v\n", "SpaceX", 94)
 fmt.Printf("%-15v $%4v\n", "Virgin Galactic", 100)
}

运行

Code/go/hello via 🐹 v1.20.3 
➜ go run main.go
SpaceX          $  94
Virgin Galactic $ 100

Code/go/hello via 🐹 v1.20.3 
➜ 

常量 和 变量

  • const,用来声明常量
  • 常量的值不可以改变
  • var,用来声明变量
  • 想要使用变量首先需要进行声明
// My weight loss program.
package main

import "fmt"

// main is the function where it all begins.
func main() {
 const lightSpeed = 200792 // km/s
 var distance = 56000000   // km

 fmt.Println(distance/lightSpeed, "seconds")

 distance = 401000000
 fmt.Println(distance/lightSpeed, "seconds")
}

运行

Code/go/hello via 🐹 v1.20.3 
➜ go run main.go
278 seconds
1997 seconds

Code/go/hello via 🐹 v1.20.3 
➜ 

同时声明多个变量

// My weight loss program.
package main

import "fmt"

// main is the function where it all begins.
func main() {
 var distance = 56000000
 var speed = 100800

 var (
  distance = 56000000
  speed    = 100800
 )

 var distance, speed = 56000000, 100800

 const hoursPerDay, minutesPerHour = 24, 60
}

赋值运算符

// My weight loss program.
package main

// main is the function where it all begins.
func main() {
 var weight = 149.0
 weight = weight * 0.3783
 weight *= 0.3783
}

自增运算符

  • 但是Go里面没有类似C#的++count这种操作。
// My weight loss program.
package main

// main is the function where it all begins.
func main() {
 var age = 41
 age = age + 1
 age += 1
 age++
}

猜数

  • 使用rand包,可以生成伪随机数
  • 例如,Intn可以返回一个指定范围的随机整数
  • import的路径是 “math/rand”
// My weight loss program.
package main

import (
 "fmt"
 "math/rand"
)

// main is the function where it all begins.
func main() {
 var num = rand.Intn(10) + 1
 fmt.Println("num = ", num)

 num = rand.Intn(10) + 1
 fmt.Println("num = ", num)
}

运行

Code/go/hello via 🐹 v1.20.3 
➜ go run main.go
num =  10
num =  3

Code/go/hello via 🐹 v1.20.3 

作业题

  • Malacandra是C.S. Lewis在《太空三部曲》中给火星起的另一个名字。编写程序来确定飞船要在28天内到达Malacandra的行进速度(公里/小时)。假设距离为56,000,000公里。
// My weight loss program.
package main

import (
 "fmt"
)

// main is the function where it all begins.
func main() {
 var kilometer = 56000000
 var day = 28
 var speed = kilometer / (day * 24)

 fmt.Println("speed = ", speed)
}

总结

通过本篇文章,你已经掌握了 Go 语言的基础安装步骤以及如何编写和运行简单的 Go 程序。你学会了如何使用常见的算术运算符、格式化输出、声明变量和常量等。接下来,你可以进一步探索 Go 语言的并发编程、内存管理和更高级的功能。无论你是从事后端开发、云计算还是系统编程,Go 语言都能帮助你提升开发效率,创造出更加高效可靠的应用。

全部评论(0)