01 Go 基础知识学习笔记

📅 发布时间:2026/8/11 6:15:38
01 Go 基础知识学习笔记
Go 基础知识学习笔记1. 包声明Package Declaration每个 Go 文件必须以package声明开头表示该文件属于哪个包。可独立执行的 Go 程序必须有一个package main声明。packagemainimportfmtfuncmain(){fmt.Println(Package declaration is working!)}执行结果Package declaration is working!要点package main是可执行程序的入口包编译后生成可执行文件其他包名如package math是库包不能直接运行同一个目录下的所有.go文件必须声明相同的包名2. 导入包ImportGo 使用import关键字引入其他包的功能。单个包用单行导入packagemainimportfmtfuncmain(){fmt.Println(Learning about imports!)}执行结果Learning about imports!要点import fmt引入标准库的格式化输出包导入但未使用的包会导致编译错误Go 不允许无用导入3. 多包导入Multiple Imports导入多个包时推荐使用括号分组写法packagemainimport(fmttime)funcmain(){fmt.Println(Current time:)fmt.Println(time.Now())}执行结果Current time: 2026-07-23 17:16:53.5315386 0800 CST m0.000529001要点分组导入比逐行写多个import更清晰是 Go 推荐风格time.Now()返回当前时间fmt.Println会自动格式化输出导入的每个包都必须被使用否则编译报错4. main 函数 — 程序入口func main()是可执行程序的唯一入口点程序从这里开始执行packagemainimportfmt// Fixed: Created the main function and moved the print statement insidefuncmain(){fmt.Println(This is the main function!)}执行结果This is the main function!要点func main()不接收参数也不返回值没有main函数的 Go 程序无法编译运行所有可执行代码都必须在函数内部不能像脚本语言那样在函数外直接写语句5. Hello World — 第一个程序packagemainimportfmt// Fixed: Changed the output to Hello, GoForGo! as requestedfuncmain(){fmt.Println(Hello, GoForGo!)}执行结果Hello, GoForGo!要点这是 Go 最简单的完整程序包声明 → 导入 → main 函数 → 一行输出fmt.Println输出字符串并自动换行6. 注释CommentsGo 支持两种注释方式单行注释和多行注释。packagemainimportfmt// This is a single-line comment/* This program demonstrates the use of comments in Go. It prints a message about code readability. */funcmain(){fmt.Println(Comments help make code readable!)// This line prints a message to the console}执行结果Comments help make code readable!要点//是单行注释从//到行尾的内容被编译器忽略/* ... */是多行注释可以跨越多行注释不影响程序执行但能提高代码可读性Go 文档工具godoc会提取注释生成文档7. 输出函数Print Functionsfmt包提供了多种输出函数最常用的是Println和Printfpackagemainimportfmtfuncmain(){name:Gopherage:5// Fixed: Using fmt.Printf with format specifiersfmt.Printf(Hello, my name is %s and I am %d years old.\n,name,age)}执行结果Hello, my name is Gopher and I am 5 years old.要点fmt.Println()— 输出值并自动换行多个参数之间自动加空格fmt.Printf()— 格式化输出需要手动加\n换行常用格式化动词%s— 字符串%d— 整数%v— 通用格式任何类型%T— 输出值的类型%f— 浮点数\n— 换行符8. 程序结构Program Structure一个完整的 Go 程序由三个部分组成顺序固定包声明 → 导入 → 函数定义packagemainimportfmt/* This program demonstrates the basic structure of a Go program. It shows the proper order: package declaration, imports, then functions. */funcmain(){fmt.Println(Understanding Go program structure!)fmt.Println(Package - Imports - Functions - Code)}执行结果Understanding Go program structure! Package - Imports - Functions - Code要点Go 程序的固定顺序package→import→funcpackage必须是第一行除了注释import必须在package之后、函数定义之前函数定义必须在import之后9. 格式化输出与多包协作Formatting结合多个包进行格式化输出packagemainimport(fmttime)funcmain(){message:Welcome to Go!fmt.Println(message)fmt.Println(Current time:,time.Now())}执行结果Welcome to Go! Current time: 2026-07-23 17:16:59.2784201 0800 CST m0.000511401要点:是短变量声明只能在函数内部使用fmt.Println多参数输出时参数之间自动插入空格不同包的功能可以组合使用fmt负责输出time提供时间数据10. 分号规则SemicolonsGo 的语法不需要手动添加分号编译器会自动插入packagemainimportfmtfuncmain(){// Fixed: Removed unnecessary semicolonsfmt.Println(First line)fmt.Println(Second line)fmt.Println(Third line)}执行结果First line Second line Third line要点Go 不要求语句末尾加分号和 C/Java 不同编译器的分号插入规则如果一行末尾是标识符、数字、字符串、)等特定 token自动插入分号因此{不能写在下一行否则编译器会在上一行末尾插入分号导致语法错误正确写法funcmain(){// { 必须和 func main 在同一行// ...}错误写法funcmain()// 编译器会在此行末尾自动插入分号导致语法错误{// 这行会报 unexpected semicolon or newline before {// ...}知识点总结知识点关键概念包声明package main是可执行程序的入口包同一目录下文件包名必须相同单包导入import fmt引入标准库包未使用的包会导致编译错误多包导入import (fmt time)分组写法Go 推荐风格main 函数func main()是程序唯一入口无参数无返回值Hello World最简程序package → import → main → Println注释//单行注释/* */多行注释不影响执行输出函数Println自动换行加空格Printf格式化输出需手动\n程序结构固定顺序package → import → func格式化输出:短变量声明多包功能组合使用分号规则Go 不需要手动加分号{必须与函数声明在同一行