Go语言现代Web开发03 关键字和包以及基本数据类型

关键字

Keywords are special words that help the compiler to understand and properly parse code.

关键字是帮助编译器理解和正确解析代码的特殊单词。

Currently, Go has 25 keywords that can be classified into four categories:

  • Keywords used for declaration: const, func, import, package, type and var.
  • Keywords used in composite type denotations: chan, interface, map and struct.
  • Keywords used for folow control: break, case, continue, default, defer, else, fallthrough, for, goto, if, range, return, select, and switch.

目前,Go有25个关键词,可以分为四类:

  • 用于声明的关键字:const, func, import, package, type和var。
  • 复合类型表示中使用的关键字:chan, interface, map和struct。
  • 用于流程控制的关键字:break、case、continue、default、defer、else、fallthrough、for、goto、if、range、return、select和switch。

In this and the next chapter, we will see how most of these keywords can be used.

在本章和下一章中,我们将看到这些关键字是如何使用的。

Packages are basic building blocks for Go Programs. All variables, constants, and functions are declared and defined in packages, and package definition is usually the first line in the file with code. This statement will define the new package with the name country.

包是Go程序的基本构建块。所有变量、常量和函数都是在包中声明和定义的,包定义通常是文件中代码的第一行。该语句将以country的名称定义新包。

bash 复制代码
package country

Each package can export things that can be used in other packages. In the Go programming language, there is no special keyword or complex syntax for export, everything inside the package that begins with a capital letter will be exported.

每个包都可以导出可以在其他包中使用的东西。在Go编程语言中,没有特殊的关键字或复杂的语法用于导出,包中以大写字母开头的所有内容都将被导出。

go 复制代码
const Exported = 33
const nonExported = 333

Everything exported from the package can be imported and used in other packages, these statements will import packages country and math (Go package with basic constants and mathematical functions):

从包中导出的所有内容都可以导入并在其他包中使用,这些语句将导入包country和math(带有基本常量和数学函数的Go包):

go 复制代码
import "country"
import "math"

Imports can be grouped in order to avoid writing the import keyword multiple times. This grouped import is equivalent to import statements from the previous example:

可以对导入进行分组,以避免多次写入import关键字。这个分组导入相当于上一个例子中的import语句:

go 复制代码
import (
    "country"
    "math"
)

All Go programs will start running the main package (which must contain main() function).

所有Go程序都将开始运行main包(必须包含main()函数)。

基础数据类型

Go basic data types can be separated into three categories.

基本数据类型可以分为三类。

Numberical data types: These are used to represent different kinds of numbers. We can further classify numerical data types into more specific types.

数字数据类型:用于表示不同类型的数字。我们可以进一步将数值数据类型划分为更具体的类型。

  • Integers: Representation of whole numbers. Integers can be positive, negative, or zero. Examples of integers are -27, 0, 21, 1999, 180111 and so on. Based on the number of bits used to store integer values, the Go programming language supports the following types: int8 (8 bits), int16(16 bits), int32(32 bits), int64(64 bits), and int(32 bits on 32-bit systems, 64-bits on 64-bit systems). A good practice is to use int unless we have some specific erason to use a specific size.

  • Un signed integers: Positive whole numbers and zero. Based on the number of bits used to store unsigned integer values, the Go programming language supports the following types: uint8(8 bits), uint16(16 bits), uint32(32 bits), uint64(64 bits), uintptr(32 bits on 32-bit systems, 64 bits on 64-bit systems), and uint(32 bits on 32-bit systems, 64 bits on 64-bit systems). Type uintptr is an unsigned integer that is large enough to hold any pointer address(pointers will be explained later). Generally, int should be used whenever we need to use whole numbers and unsigned integers should be used only for some specific use cases.

  • Floating point numbers: Representation of whole numbers with a decimal point. Floating point numbers can be positive, negative, or zero. Examples of floating points are -33.333, 0.0, 33.33 and so on. Based on the number of bits used to store floating point numbers the Go programming language supports two types: float32(32 bits) and float64(64 bits).

  • Complex numbers: Numbers that can be presented in the form a + bi, where a and b are real numbers and i is a solution of equation x2 = -1. Real numbers a and b are referred to as a real and imaginary parts respectively. Based on the number of bis used to store floating point numbers, the Go programming language supports two types: complex64(64 bits) and complex128(128 bits).

  • Boolean data type: A data type that has one of the two possible values, true or false. It is widely used for logical operations, we will see a lot of examples later in this chapter. Go programming language supports one Boolean data type: bool.

  • String data type: Sequence of alphanumberic text or other symbols. Examples of strings are "Hello World!" and so on. Strings are mostly used for processing all kinds of textual data or to display different kinds of inofrmation to the users of the application (we will see more of that in this and the following chapters). Go programming language supports one type: string.

  • 整数:表示整数。整数可以是正的、负的或零。整数的例子有-27、0、21、1999、180111等。根据存储整数值的位数,Go编程语言支持以下类型:int8(8位)、int16(16位)、int32(32位)、int64(64位)和int(32位系统为32位,64位系统为64位)。一个好的做法是使用int,除非我们有一些特定的擦除来使用特定的大小。

  • 无符号整数:正整数和零。根据用于存储无符号整数值的位数,Go编程语言支持以下类型:uint8(8位)、uint16(16位)、uint32(32位)、uint64(64位)、uintptr(32位,64位)和uint(32位,64位)。uintptr类型是一个无符号整数,它足够大,可以容纳任何指针地址(指针将在后面解释)。一般来说,只要我们需要使用整数,就应该使用int,而无符号整数只应该用于某些特定的用例。

  • 浮点数:用小数点表示整数。浮点数可以是正的、负的或零。浮点数的例子有-33.333、0.0、33.33等等。根据用于存储浮点数的位数,Go编程语言支持两种类型:float32(32位)和float64(64位)。

  • 复数:可以用a + bi的形式表示的数,其中a和b是实数,i是方程x2 = -1的解。实数a和b分别称为实部和虚部。根据用于存储浮点数的二进制数的数量,Go编程语言支持两种类型:complex64(64位)和complex128(128位)。

  • 布尔数据类型:具有true或false两种可能值之一的数据类型。它被广泛用于逻辑运算,我们将在本章后面看到很多例子。Go编程语言支持一种布尔数据类型:bool。

  • 字符串数据类型:由字母数字或其他符号组成的序列。字符串的例子有"Hello World!"等等。字符串主要用于处理各种文本数据或向应用程序的用户显示不同类型的信息(我们将在本章和以下章节中看到更多)。Go编程语言支持一种类型:字符串。

String is a sequence of characters, but Go has no data type that represents a single character (char in other programming languages). Characters are supported through two special aliases of integer types:

  • Byte: Alias for uint8, represents ASCII character (by ASCII standard, each character is a 8-bit code).
  • Rune: Alias for int32, represents Unicode character encoded in UTF-8 format.

字符串是一个字符序列,但是Go没有表示单个字符(在其他编程语言中是char)的数据类型。通过整数类型的两个特殊别名支持字符:

  • Byte: uint8的别名,表示ASCII字符(按照ASCII标准,每个字符是一个8位代码)。
  • Rune: int32的别名,表示以UTF-8格式编码的Unicode字符。

These aliases are introduced to make a clear distinction between characters and integer values. Generally, it will be hard to understand and maintain code where we use integer variables to store characters. Rune is a default type for characters, so if we do not explicitly declare the type for the variable with character value, Go wll assign rune type to that variable (we will learn how to declare a variable soon).

引入这些别名是为了明确区分字符和整数值。通常,使用整型变量存储字符的代码很难理解和维护。Rune是字符的默认类型,所以如果我们没有显式地为带有字符值的变量声明类型,Go将为该变量分配Rune类型(我们将很快学习如何声明变量)。

All basic data types have a default value (this is often referred to in the documentation for Go programming language as zero value). The default value is 0 for numberic types, "" (empty string) for strings and false for Boolean.

所有基本数据类型都有一个默认值(在Go编程语言的文档中,这通常被称为零值)。数字类型的默认值为0,字符串类型的默认值为""(空字符串),布尔类型的默认值为false。

If we do not assign a specific value to the variable, the default one will be assigned. There is no special value for that situation (like undefined) and only pointers can have nil value. We will talk more about pointers later in this chapter.

如果我们不给变量赋一个特定的值,则会赋一个默认值。这种情况下没有特殊的值(如未定义),只有指针可以为nil值。我们将在本章后面更多地讨论指针。

相关推荐
敲代码不忘补水2 分钟前
二十种编程语言庆祝中秋节
java·javascript·python·golang·html
0点51 胜7 分钟前
[ffmpeg]音频格式转换
开发语言·c++·ffmpeg
水木流年追梦18 分钟前
【python因果推断库16】使用 PyMC 模型进行回归拐点设计
开发语言·python·回归
只想摆烂@1 小时前
C# winfrom 如何多窗体优雅的回调方法
开发语言·c#
西猫雷婶1 小时前
python画图|中秋到了,尝试画个月亮(球体画法)
开发语言·python
星迹日1 小时前
C语言:结构体
c语言·开发语言·经验分享·笔记
会敲代码的小张1 小时前
设计模式-观察者模式
java·开发语言·后端·观察者模式·设计模式·代理模式
宗浩多捞1 小时前
C++设计模式(更新中)
开发语言·c++·设计模式
学习使我变快乐3 小时前
C++:析构函数
开发语言·c++
我言秋日胜春朝★3 小时前
【C++】继承详解
开发语言·c++