# 1 标识符、标识符控制权限

\[TOC]

## 标识符

**自定义标识符**

> * 自定义的变量名、常量名、函数名等。
> * 自定义标识符不能包括关键字，为避免产生歧义和影响代码可读性也不该使用语言预留标识符。
> * 标准标识符使用字母开头，区分大小写。使用下划线或Unicode字符中文等文字开头是合法的但是一般不会这么定义。后面可以使用字母、数字、下划线和Unicode字符。
> * 自定义标识符采用驼峰原则。

| 标识符     | 合法  | 原因          |
| ------- | --- | ----------- |
| aBcDe   | 合法  | 标准的，符合驼峰原则  |
| Abc     | 合法  |             |
| abc     | 合法  |             |
| \_abc   | 合法  |             |
| ab2     | 合法  |             |
| 中文      | 合法  |             |
| a中文     | 合法  |             |
| int     | 合法  | 数据类型标识符，不推荐 |
| 1abc    | 不合法 | 不能以数字开头     |
| +ab     | 不合法 | 不能以+开头      |
| package | 不合法 | 关键字         |

**关键字**

> * 25个不能作为标识符的关键字。

| 关键字                                         | 作用            |
| ------------------------------------------- | ------------- |
| package                                     | 定义包名          |
| import                                      | 导入包           |
| const                                       | 常量声明          |
| var                                         | 变量声明          |
| func                                        | 函数定义          |
| defer                                       | 延迟执行          |
| go                                          | 开启协程          |
| return                                      | 函数、方法返回       |
| struct                                      | 定义结构体         |
| interface                                   | 定义接口          |
| map                                         | 声明或创建map      |
| chan                                        | 声明或创建通道       |
| if else                                     | 判断语句          |
| for range break continue                    | 循环语句          |
| switch select type case default fallthrough | switch和select |
| goto                                        | goto语句        |

**内置数据类型标识符**

> * 20个，不推荐作为标识符。

| 内置数据类型标识符                                                               | 作用     |
| ----------------------------------------------------------------------- | ------ |
| byte int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintprt | 12个整形  |
| float32 float64                                                         | 2个浮点型  |
| complex64 complex128                                                    | 2个复数型  |
| string rune                                                             | 字符和字符串 |
| error                                                                   | 接口     |
| bool                                                                    | 布尔     |

**内置函数标识符**

> * 15个内置函数标识符。
> * 内置函数是高级语言的一种语法糖，小写字母开头依然全局可用不需要import。

| 内置函数标识符 | 作用                                                             |
| ------- | -------------------------------------------------------------- |
| make    | 分配空间、初始化 slice、map、chan。                                       |
| new     | 分配任意类型空间，返回指针。                                                 |
| len     | 计算数组、数组指针、slice、map、chan、string等数据类型的长度，struct、整型等不能作为len函数参数。 |
| cap     | 数组、切片、chan分配的空间大小。                                             |
| append  | 追加slice，自动扩容。                                                  |
| copy    | 只用于slice拷贝                                                     |
| delete  | delete(map,key) 删除map键值对                                       |
| panic   | 触发异常                                                           |
| recover | 捕获异常                                                           |
| close   | 关闭chan                                                         |
| complex | 复数处理                                                           |
| real    | 复数处理                                                           |
| image   | 复数处理                                                           |
| print   | 打印函数                                                           |
| printin | 打印函数                                                           |

**常量值标识符**

> * 4个常量值标识符

| 常量值标识符     | 作用           |
| ---------- | ------------ |
| true false | bool类型的两个常量值 |
| iota       | 连续枚举类型声明     |
| nil        | 指针、引用类型的默认值  |

**空白标识符 \_**

> * 1个空白标识符 \_ 。
> * 空白标识符用于声明一个匿名的变量，或称占位符，忽略多返回值函数的一个返回值。
> * 如 file,\_ := os.Open(filePath) 忽略Open第二个返回值error。

## 标识符控制权限

**标识符控制权限**

> * golang通过标识符首字母大小写控制权限，标识符首字母大写为公开，首字母小写或是下划线、中文等其它Unicode字符都是私有。
> * golang通过包管理程序代码，私有的变量、函数、方法别的包无法访问。

## 字面常量

> * 代码中固定值的符号称为字面常量。如果没有显式指定字面常量类型，Go编译器会结合字面常量的值自动进行类型推断。

```
"str" 为string类型字面常量
if "str" == "str" {}
88，99 都是int类型字面常量
if 88 == 99 {}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pandaman.gitbook.golang.aboshe.cn/1-biao-shi-fu-biao-shi-fu-kong-zhi-quan-xian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
