Go 语言编程 — 程序结构

目录

Hello World

package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
}

使用 go run 指令执行程序:

$ go run hello.go 

使用 go build 指令生成二进制:

$ go build hello.go 

$ ll
total 4256
-rwxr-xr-x  1 mickeyfan  staff   2.1M  7  5 11:08 hello
-rw-r--r--  1 mickeyfan  staff    76B  7  5 11:08 hello.go

$ ./hello

程序结构

Go 程序的基本组成部分,包括:

  • 包声明
  • 导入包
  • 函数
  • 变量
  • 表达式
  • 语句
  • 注释

包声明

必须在源文件中非注释的第一行指明这个文件属于哪个包,使用 package 关键字,如:package main,表示一个可独立执行的 main 程序,每个 Go 应用程序都包含一个名为 main 的包。

注意

  • 文件名与包名没有直接关系,并非需要一致。
  • 文件夹名与包名没有直接关系,并非需要一致。
  • 同一个文件夹下的文件只能有一个包名,否则编译报错。

导入包

使用 package 关键字定义的包,可以被其他包导入,使用 import 关键字。例如:import “fmt”,表示告诉 Go 编译器当前这个 main 包需要使用到 fmt 包中的函数,或其他元素。fmt 包实现了格式化 IO(输入/输出)的函数。

函数

使用 func 关键字来修饰一个函数的定义,例如:func main(),main 函数是每一个可执行程序所必须包含的,作为程序的入口。但 Golang 还定义了 init() 初始化函数的概念,若存在则最优先执行。

标识符

常量名、变量名、类型、函数名、结构体名称都统称之为标识符。Golang 标识符由 A-Z、a-z、0-9、_(下划线)任意组成,但第一个字符必须是字母或下划线而不能是数字。

当标识符以一个大写字母开头,如:Group1,表示其可以被外部包导入并使用,相对于当前包而言,这被称为导出。当标识符以小写字母开头,如:group1,则对外部包是不可见的,仅在当前包内部可见。

关键字

在这里插入图片描述

语句

一行代表一个语句结束。每个语句间不需要行间隔符 “;”,这些工作将由 Go 编译器自动完成。如果你希望将多个语句写在同一行,它们则必须使用 “;” 人为区分,但在实际开发中并不鼓励这种做法。

表达式

表达式通常由标识符和运算符组成,与语句的本质区别的与:表达是一定有返回值,而语句不一定。

注释

Golang 的注释和 C 语言一般:

  • 单行注释://
  • 块(多行)注释:/**/
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
go语言系统编程,是英文版, What this book covers Chapter 1, Getting started with Go and Unix Systems Programming, starts by defining what systems programming is before talking about the advantages and the disadvantages of Go, the features of Go version 1.8, two handy Go tools named gofmt and godoc, as well as the various states of Unix processes. Chapter 2, Writing Programs in Go, helps you learn how to compile Go code and how to use the environment variables that Go supports, and understand how Go reads the command line arguments of a program. Then, we will talk about getting user input and output, which are fundamental tasks, show you how to define functions in Go, where the defer keyword is mentioned for the first time in this book and continue by discussing the data structures that Go offers using handy code examples. In the remaining sections of the chapter, we will discuss Go interfaces and random number generation. I am sure that you are going to enjoy this chapter! Chapter 3, Advanced Go Features, goes deeper and starts talking about some advanced Go features, including error handling, which is critical when developing systems software and error logging. Then it introduces you to pattern matching and regular expressions, Go Reflection, and talks about unsafe code. After that, it compares Go to other programming languages and presents two utilities, named dtrace(1) and strace(1), that allow you to see what happens behind the scenes when you execute a program. Lastly, it talks about how you can use the go tool to detect unreachable code and how to avoid some common Go mistakes. Chapter 4, Go Packages, Algorithms, and Data Structures, talks about algorithms and sorting in Go and about the sort.Slice() function, which requires Go version 1.8 or newer. Then it shows Go implementations of a linked list, a binary tree and a hash table. After that, it discusses Go packages and teaches you how to create and use your own Go packages. The last part of the chapter discusses Garbage collection in Go. Chapter 5, Files and Directories, is the first chapter of this book that deals with a systems programming topic, which is the handling of files, symbolic links, and directories. In this chapter, you will find Go implementations of the core functionality of Unix tools such as which(1), pwd(1), and find(1), but first you will learn how to use the flag package in order to parse the command-line arguments and options of a Go program. Additionally, you will learn how to delete, rename, and move files as well as how to traverse directory structures the Go way. The last part of this chapter implements a utility that creates a copy of all the directories of a directory structure! Chapter 6, File Input and Output, shows you how to read the contents of a file, how to change them, and how to write your own data to files! In this chapter, you will learn about the io package, the io.Writer and io.Reader interfaces, and the bufio package that is used for buffered input and output. You will also create Go versions of the cp(1), wc(1), and dd(1) utilities. Lastly, you will learn about sparse files, how to create sparse files in Go, how to read and write records from files, and how to lock files in Go. Chapter 7, Working with System Files, teaches you how to deal with Unix system files, which includes writing data to Unix log files, appending data to existing files, and altering the data of text files. In this chapter, you will also learn about the log and log/syslog standard Go packages, about Unix file permissions, and take your pattern matching and regular expressions knowledge even further using practical examples. You will also learn about finding the user ID of a user as well as the Unix groups a user belongs to. Lastly, you will discover how to work with dates and times in Go using the time package and how to create and rotate log files on your own. Chapter 8, Processes and Signals, begins by discussing the handling of Unix signals in Go with the help of the os/signal package by presenting three Go programs. Then it shows a Go program that can rotate its log files using signals and signal handling and another Go program that uses signals to present the progress of a file copy operation. This chapter will also teach you how to plot data in Go and how to implement Unix pipes in Go. Then it will implement the cat(1) utility in Go before briefly presenting the Go code of a Unix socket client. The last section of the chapter quickly discusses how you can program a Unix shell in Go. Chapter 9, Goroutines – Basic Features, discusses a very important Go topic, which is goroutines, by talking about how you can create goroutines and how you can synchronize them and wait for them to finish before ending a program. Then it talks about channels and pipelines, which help goroutines communicate and exchange data in a safe way. The last part of the chapter presents a version of the wc(1) utility that is implemented using goroutines. However, as goroutines is a big subject, the next chapter will continue talking about them. Chapter 10, Goroutines – Advanced Features, talks about more advanced topics related to goroutines and channels, including buffered channels, signal channels, nil channels, channels of channels, timeouts, and the select keyword. Then it discusses issues related to shared memory and mutexes before presenting two more Go versions of the wc(1) utility that use channels and shared memory. Lastly, this chapter will talk about race conditions and the GOMAXPROCS environment variable. Chapter 11, Writing Web Applications in Go, talks about developing web applications and web servers and clients in Go. Additionally, it talks about communicating with MongoDB and MySQL databases using Go code. Then, it illustrates how to use the html/template package, which is part of the Go standard library and allows you to generate HTML output using Go HTML template files. Lastly, it talks about reading and writing JSON data before presenting a utility that reads a number of web pages and returns the number of times a given keyword was found in those web pages. Chapter 12, Network Programming, discusses topics related to TCP/IP and its protocols using the net Go standard package. It shows you how to create TCP and UDP clients and servers, how to perform various types of DNS lookups, and how to use Wireshark to inspect network traffic. Additionally, it talks about developing RPC clients and servers in Go as well as developing a Unix socket server and a Unix socket client. As you will see, at the end of each chapter there are some exercises for you to do in order to gain more information about important Go packages and write your own Go programs. Please, try to do all the exercises of this book

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

范桂飓

文章对您有帮助就请一键三连:)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值