规范的代码风格要求 | 学习笔记

简介: 简介:快速学习规范的代码风格要求

开发者学堂课程【Go 语言核心编程 - 基础语法、数组、切片、Map :规范的代码风格要求】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/625/detail/9505


规范的代码风格要求

 

内容介绍

一、正确的注释和注释风格

二、正确的缩进和空白

三、代码风格


一、正确的注释和注释风格

1) Go 官方推荐使用行注释来注释整个方法和语句。

2) 带看 Go 源码

图为 go 中的源代码,可以看出基本使用的都为行注释,可以看出原作者的注释也以行注释为主,因而我们借鉴源代码,以行注释为主。


二、正确的缩进和空白

1.示例:

如以下代码,适当添加缩进会较为美观

l 变化前:

package main

import "fmt”  

//fmt表示格式化的包包中提供格式化,输出,输入的函数

func main() {

//演示转义字符的使用 \t

//如果希望一次性注释ctrl+/ 第一次表示注释,第二次表示取消注释

fmt.Println("tomtjack")

fmt.Println( "hello\nworld") // \n表示换行输出

fmt.Println("c: \\users\\Administrator\\Desktop\\Go语言核心编程课程\\资料")

fmt.Println( "tom说\"i love you\"")

fmt.Println("天龙八部雪山飞狐\r张飞")

}

l 变化后--有正确的缩进,代码更加清楚

package main

import "fmt”  

//fmt表示格式化的包包中提供格式化,输出,输入的函数

func main() {

//演示转义字符的使用 \t

//如果希望一次性注释ctrl+/ 第一次表示注释,第二次表示取消注释

fmt.Println("tomtjack")

fmt.Println( "hello\nworld") // \n表示换行输出

fmt.Println("c: \\users\\Administrator\\Desktop\\Go语言核心编程课程\\资料")

fmt.Println( "tom说\"i love you\"")

fmt.Println("天龙八部雪山飞狐\r张飞")

}

2.使用

1)使用一次 tab 操作,实现缩进,默认整体向右边移动,时候用 shift+tab 整体向左移

l 举例

假设此时的11行到13行排版稍乱,只需全部选中,按下 shift+tab 就能够快速整体向左移动,再按 tab 键即可向右移动至格式工整。

① 代码如下:

package main

import "fmt”  

//fmt表示格式化的包包中提供格式化,输出,输入的函数

func main() {

//演示转义字符的使用 \t

/如果希望一次性注释ctrl+/ 第一次表示注释,第二次表示取消注释

fmt.Println("tomtjack")

fmt.Println( "hello\nworld") // \n表示换行输出

fmt.Println("c: \\users\\Administrator\\Desktop\\Go语言核心编程课程\\资料")

fmt.Println( "tom说\"i love you\"")

fmt.Println("天龙八部雪山飞狐\r张飞")

}

调整前

package main

import "fmt”  

//fmt表示格式化的包包中提供格式化,输出,输入的函数

func main() {

//演示转义字符的使用 \t

//如果希望一次性注释ctrl+/ 第一次表示注释,第二次表示取消注释

fmt.Println("tomtjack")

fmt.Println( "hello\nworld") // \n表示换行输出

fmt.Println("c: \\users\\Administrator\\Desktop\\Go语言核心编程课程\\资料")

fmt.Println( "tom说\"i love you\"")

fmt.Println("天龙八部雪山飞狐\r张飞")

}

(调整后)

2)或者使用 gofmt 来进行格式化[演示]

l 举例

按照刚才代码案例,将代码任意缩进。

package main

import "fmt”  

//fmt表示格式化的包包中提供格式化,输出,输入的函数

func main() {

//演示转义字符的使用 \t

//如果希望一次性注释ctrl+/ 第一次表示注释,第二次表示取消注释

fmt.Println("tomtjack")

fmt.Println( "hello\nworld") // \n表示换行输出

fmt.Println("c: \\users\\Administrator\\Desktop\\Go语言核心编程课程\\资料")

fmt.Println( "tom说\"i love you\"")

fmt.Println("天龙八部雪山飞狐\r张飞")

}

( 执行命令前 )

在命令窗口中输入 dos 令:

gofmt main.go,执行成功后,即显示出结果,可看出代码格式已被对齐,但此时原文件中的代码格式并未发生改变,仅执行 gofmt 命令后的输出结果格式改变。

image.png

将原文件中代码格式改变,需再输入 dos 命令:gofmt -w main.go

执行后,适当切换文件,再次打开即可看到改变格式的代码(如图)。

package main

import "fmt”  

//fmt表示格式化的包包中提供格式化,输出,输入的函数

func main() {

//演示转义字符的使用 \t

//如果希望一次性注释ctrl+/ 第一次表示注释,第二次表示取消注释

fmt.Println("tomtjack")

fmt.Println( "hello\nworld") // \n表示换行输出

fmt.Println("c: \\users\\Administrator\\Desktop\\Go语言核心编程课程\\资料")

fmt.Println( "tom说\"i love you\"")

fmt.Println("天龙八部雪山飞狐\r张飞")

}

( 执行命令后 )

l 总结:

gofmt 只是将格式化后的内容输出,gofmt -w 是将格式化后的内容重新写入到文件,实现真正的格式改变。当程序员重新打开文件时,就会看到新的格式化后的文件。

3)运算符两边习惯性各加一个空格。比如:2+4* 5。

l 举例:

假设定义一个名为num的变量,值为2+4*5的结果,书写时运算符两边都含有空格。

var num = 2 + 4 * 5

同时,在 go 的源码之中也可以看出,运算符两边需要加上空格。


三、代码风格

1.{}的写法

(1) 错误写法

package main

import "fmt"

func main()

{

fmt.PrintIn("hello,world!")

}

[Go语言不允许这样写, 是错误的! ]

(2) 正确写法

package main

import "fmt"

func main(){

fmt.PrintIn("hello,world!")

}

【该种写法时正确】

原因:

因为 Go 语言设计者认为:一个问题尽量只有一个解决方法,所以认定正确的书写方法只有一种。

(3) 示例

l 代码

package main

import "fmt”  

//fmt表示格式化的包包中提供格式化,输出,输入的函数

func main()

{   //错误,如果将func main()后的{  移至该行,就会报错

//演示转义字符的使用 \t

//如果希望一次性注释ctrl+/ 第一次表示注释,第二次表示取消注释

fmt.Println("tomtjack")

fmt.Println( "hello\nworld") // \n表示换行输出

fmt.Println("c: \\users\\Administrator\\Desktop\\Go语言核心编程课程\\资料")

fmt.Println( "tom说\"i love you\"")

fmt.Println("天龙八部雪山飞狐\r张飞")

}

l 输出结果

报错,因为{}的格式错误 

2. 行长约定

一行最长不超过80个字符,超过的请使用换行展示,尽量保持格式优雅。

(1) 示例

当代码很长,不便查看时。fmt.Println("helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld)

可以代码分成几部分用“”括起,再放至下一行,同时使用逗号连接,其中逗号类似与之前提过的+号。

fmt.Println("helloworldhelloworldhelloworldhelloworl",

"dhelloworldhelloworldhelloworldhelloworldhelloworldhellowor","ldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworl","dhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhel","loworldhelloworldhelloworldhelloworldhelloworldhelloworldhellowor","ldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld")

输出结果:仍然与没有添加逗号之前的输出结果一致,输出结果全为一行。

为使显示更加美观,用换行输出,在各行最后添加\n即可

fmt.Println("helloworldhelloworldhelloworldhelloworl","dhelloworldhelloworldhelloworldhelloworldhelloworldhellowor\n","ldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworl\n","dhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhel\n","loworldhelloworldhelloworldhelloworldhelloworldhelloworldhellowor\n","ldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld\n")

输出结果:各行就会依次换行输出

相关文章
|
8月前
|
缓存 运维 前端开发
快速定位进程性能瓶颈
这篇文章详细介绍了进程热点追踪的概念、业务痛点、解决方案以及实际案例分析,旨在帮助开发者和运维人员快速定位和解决系统性能瓶颈问题。
快速定位进程性能瓶颈
|
8月前
|
Web App开发 前端开发 JavaScript
《前端定位探秘:fixed定位的深度剖析与transform的神秘影响》
本文深入探讨了CSS中fixed定位的原理及其与祖先元素transform属性的交互关系。fixed定位通常以视口为参考,使元素固定于屏幕特定位置,广泛用于导航栏、悬浮按钮等场景。然而,当祖先元素应用了transform(如平移、旋转、缩放)时,会创建新的堆叠上下文和包含块,导致fixed定位元素的参照系从视口切换到该祖先元素,从而改变其行为。
240 18
|
SQL 关系型数据库 MySQL
在 MySQL 中使用子查询
【8月更文挑战第12天】
667 0
在 MySQL 中使用子查询
|
9月前
|
人工智能 安全 物联网
《鸿蒙系统中人工智能驱动的智能助手:应用模式与未来航向》
在数字化时代,人工智能与操作系统的融合成为科技变革的核心力量。鸿蒙系统作为华为自主研发的分布式操作系统,为智能助手提供了广阔舞台。通过语音交互、多模态融合、场景感知与跨设备协同,智能助手实现了便捷操控、深度交互和主动服务。未来,借助大模型赋能、物联网深度融合及强化隐私保护,智能助手将推动全场景服务创新,助力开发者生态繁荣,开启万物互联的智能交互新时代。
858 12
|
自动驾驶 物联网 5G
什么是 5G 以及它如何工作?
【8月更文挑战第23天】
3101 0
|
自然语言处理 关系型数据库 数据库
ElasticSearch 映射类型及数据类型区分
ElasticSearch 映射类型及数据类型区分
213 0
|
算法 API 索引
Zipline 3.0 中文文档(二)(5)
Zipline 3.0 中文文档(二)
232 2
|
11月前
|
人工智能 监控 安全
《探秘鸿蒙Next:如何保障AI模型轻量化后多设备协同功能一致》
在鸿蒙Next多设备协同中,确保轻量化AI模型功能一致性至关重要。方法包括:采用标准化框架(如TensorFlow Lite)和制定模型规范,统一数据预处理与同步机制,针对不同硬件优化模型并使其具备自适应能力,进行多设备测试、边界条件测试及用户场景模拟测试,建立运行时监控与反馈更新机制,同时保障安全与隐私。通过这些策略,形成完整技术体系,确保智能体验的稳定、高效与一致。
395 7
|
人工智能 开发框架 算法
《C++巧筑智能框架根基:开启 AI 开发新航道》
在科技飞速发展的今天,C++作为高效强大的编程语言,在构建人工智能开发框架基础架构中扮演着重要角色。本文探讨如何利用C++的优势,从数据处理、模型构建、训练及评估等模块出发,打造稳定、高效的AI开发框架,支持计算密集型任务,促进人工智能技术的发展与应用。
356 8
|
存储 JSON 物联网
查询性能提升 10 倍、存储空间节省 65%,Apache Doris 半结构化数据分析方案及典型场景
本文我们将聚焦企业最普遍使用的 JSON 数据,分别介绍业界传统方案以及 Apache Doris 半结构化数据存储分析的三种方案,并通过图表直观展示这些方案的优势与不足。同时,结合具体应用场景,分享不同需求场景下的使用方式,帮助用户快速选择最合适的 JSON 数据存储及分析方案。
640 15
查询性能提升 10 倍、存储空间节省 65%,Apache Doris 半结构化数据分析方案及典型场景