Golang基础学习总结之开篇

Go, also known as Golang, is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency

几个月前,兴趣使然,我偶然学了一个月的go语言,并开发了两个简单的小程序(短域名服务和微信记事本小程序),感受到了golang的简洁和强大。可是我并没有做一个学习总结,所以接下来,我将简单是分几篇文章来简单的总结一下golang的基础内容。
这里我们先简单列举几个Golang的优点:

  • 1、语法简洁
  • 2、强大的标准库
  • 3、性能好
  • 4、开发效率高
  • 5、天生支持并发
  • 6、编译效率高
  • ……

Hello World

一般学习一门新语言,都是从Hello world开始的,下面,我就来感受一下Golang的简洁,在这里我推荐大家使用Goland这款IDE,当然如果vs code用得很6的话也可以。

1
2
3
4
5
6
7
8
package main  //包名

import "fmt" 导入fmt包

//main主函数
func main() {
fmt.Println("Hello World") //打印输出Hello World
}

Go语言以包(package)作为管理单位,每个Go 源文件都必须先声明它所属的包,Go语言的包与文件夹是一一对应的,默认情况下同级目录下的文件属于同一个包,包名也可以与目录名不同。
main包是Go语言程序的入口包,一个Go语言程序必须有且仅有一个 main 包。如果一个程序没有 main 包,那么编译时将会出错,无法生成可执行文件。
main函数是main包里面的启动函数,如果没有main函数启动也是会报错。
go预览编译也很简单,直接到项目目录执行go build 即可在同级目录生成一个exe的可执行文件。
go build

You forgot to set the qrcode for Alipay. Please set it in _config.yml.
You forgot to set the qrcode for Wechat. Please set it in _config.yml.
You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×