仲卿 發表於 2020-4-20 20:26:00

Golang——详解Go语言代码规范

<p>本文始发于个人公众号:<strong>TechFlow</strong>,原创不易,求个关注</p>
<br>
<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: rgba(0, 0, 0, 1); padding: 0 10px; line-height: 1.6; word-spacing: 0; letter-spacing: 0; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif; margin-top: -10px"><p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">今天是<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">Golang专题的第二篇</strong>,我们来看看Go的语言规范。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">在我们继续今天的内容之前,先来回答一个问题。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">有同学在后台问我,<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">为什么说Golang更适合分布式系统的开发</strong>?它和Java相比有什么优势吗?</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">其实回答这个问题需要涉及很多概念,比如操作系统当中关于进程、线程、协程等很多概念。我们将这些内容进行简化,举一个最简单的线程的例子。我们来写一段在<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">java当中实现多线程</strong>的例子:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">public</span>&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">MyThread</span>&nbsp;<span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">implemnts</span>&nbsp;<span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Runnable</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">public</span>&nbsp;<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span>&nbsp;<span class="hljs-title" style="color: rgba(153, 0, 0, 1); font-weight: bold; line-height: 26px">run</span><span class="hljs-params" style="line-height: 26px">()</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"I&nbsp;am&nbsp;a&nbsp;thread"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">public</span>&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Test</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">public</span>&nbsp;<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">static</span>&nbsp;<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span>&nbsp;<span class="hljs-title" style="color: rgba(153, 0, 0, 1); font-weight: bold; line-height: 26px">main</span><span class="hljs-params" style="line-height: 26px">(String&nbsp;args[])</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyThread&nbsp;thread1&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">new</span>&nbsp;MyThread();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thread1.start();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">我们再来看看Golang:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">func</span>&nbsp;<span class="hljs-title" style="color: rgba(153, 0, 0, 1); font-weight: bold; line-height: 26px">run</span><span class="hljs-params" style="line-height: 26px">()</span></span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"I&nbsp;am&nbsp;a&nbsp;Thread"</span>)<br>}<br><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">func</span>&nbsp;<span class="hljs-title" style="color: rgba(153, 0, 0, 1); font-weight: bold; line-height: 26px">main</span><span class="hljs-params" style="line-height: 26px">()</span></span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">go</span>&nbsp;run()<br>}<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">这么一对比是不是简单很多?</p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; font-weight: bold; font-size: 22px; border-bottom: 2px solid rgba(89, 89, 89, 1); margin-bottom: 30px; color: rgba(89, 89, 89, 1)"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; display: inline-block; border-bottom: 2px solid rgba(89, 89, 89, 1)">Golang的语言规范</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">大家都知道程序员最大的分歧之一就是<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">花括号到底应该写在哪一行</strong>,有另写一行的,也有跟在循环体后面的。这两拨人分成了两个流派,彼此征战不休,也衍生出了许多段子。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">为了统一风格,很多语言对代码风格做了规范。比如Python就去掉了花括号,而使用空格来进行代码缩进。然而不幸的是,有些人缩进用四个空格,也有些人用tab,这双方又形成了阵营,彼此争吵不停……</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">也许Golang的开发者曾经饱受代码风格争吵的苦恼,所以Golang做了一个划时代的事情,它<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">严格限制了代码风格</strong>,强行统一大家都必须使用同一套风格,否则就分分钟报错给你看。所以在我们进行具体的语法学习之前,先从语言规范开始,否则等我们后面养成了不好的习惯再想要改正就会成本很高。其实改正代码风格是一件很难的事情,老实说我的代码风格不是很好,总是使用一些cur、pnt、node、u、v这种简单的变量,这也是当年打acm留下来的习惯,想改一时半会蛮难的。所以大家一定要在初期就养成好习惯,坏习惯就留给我一个人吧(大雾)。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; font-size: 20px; color: rgba(89, 89, 89, 1)"><span class="prefix" style="display: none"></span><span class="content">package规范</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">Golang的语言规范很多,涉及的面很广,有些我们暂时用不到,我们先挑基础的说。首先是<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">package规范</strong>,对于package来说它的名字应该和目录保持一致,采取有意义的包名,不要起一些别人看不懂的名字。比如test、unit这种,并且不能和标准库冲突。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">其次是我们在引包的时候,需要注意<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">不要使用相对路径</strong>,而应该使用绝对路径。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">//&nbsp;wrong</span><br><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span>&nbsp;<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"../../../repo"</span><br><br><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">//&nbsp;correct</span><br><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span>&nbsp;<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"github.com/repo/package<br></span></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">当然我们可以装一个<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">goimport工具</strong>,帮助我们自动引包。但是自动引包也会有坑,尤其是当目录下存在两个包名称一样的时候,有可能会引入错误,需要我们自己留意。</p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; font-weight: bold; font-size: 22px; border-bottom: 2px solid rgba(89, 89, 89, 1); margin-bottom: 30px; color: rgba(89, 89, 89, 1)"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; display: inline-block; border-bottom: 2px solid rgba(89, 89, 89, 1)">代码风格规范</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">Go语言当中规定了我们应该使用<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">驼峰标准</strong>来命名变量,不能使用_。在Go当中首字母大写表示结构体中的变量或者是包中的函数public,如果是<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">小写则表示是private</strong>,这一点尤其需要注意。刚开始写go的时候都会很不习惯,因此踩坑是常有的事。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">golang当中是有常量的,golang当中的常量一样用<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">驼峰标准,首字母大写</strong>。比如我们起一个常量叫做app_env,表示当前app运行的环境,我们必须要这样定义:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">const</span>&nbsp;AppEnv&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"env"</span><br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">另一点是Golang的设计者<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">认为行尾加上分号毫无必要</strong>,所以在编译器当中添加了会在行尾自动加上分号的功能。所以我们可以加也可以不加,但是一般认为没有必要这么做。所以普遍来说,除了在循环体或者是判断条件当中,我们一般是不写分号的。当然也有特殊情况,比如你想要把多条语句写在一行的时候:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">var</span>&nbsp;a&nbsp;<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">int</span>;&nbsp;<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">var</span>&nbsp;b&nbsp;float;<br>a&nbsp;=&nbsp;<span class="hljs-number" style="color: rgba(0, 128, 128, 1); line-height: 26px">3</span>;&nbsp;b&nbsp;=&nbsp;<span class="hljs-number" style="color: rgba(0, 128, 128, 1); line-height: 26px">3.2</span>;<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">当然还是<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">一般不推荐这么干</strong>,建议分成多行,更加美观。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">另外一点是golang当中<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">所有的变量和包都必须用上</strong>,不允许定义没有使用的东西,否则也会报错。也就是说严格限制了我们写代码时候的谨慎。不能随意申请用不到的变量,大多数语言当中没有这样的限制,但是golang当中做了限制,所以我们写代码的时候要小心。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">另外一点是关于花括号,在golang当中<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">严格限制了花括号写在当前行</strong>,而不是另起一行。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">//&nbsp;wrong</span><br><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">if</span>&nbsp;expression&nbsp;<br>{<br>&nbsp;&nbsp;...<br>}<br><br><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">//&nbsp;correct</span><br><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">if</span>&nbsp;expression&nbsp;{<br>&nbsp;&nbsp;...<br>}<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">从上面这个例子我们还可以注意到一点,就是在golang当中<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">if后面的条件不加括号</strong>,这点和Python一样。但是如果你写惯了java或者是C++刚开始可能会不太适应。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">最后一点是golang的代码规范检测工具golint当中规定了<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">所有的函数以及结构体头部必须要写注释</strong>,并且对注释的规范也进行了限制。注释的规范是<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">名称加上说明</strong>,如果不写或者是不规范的话,代码虽然可以运行,但是无法通过golint的规范检测。一般来说公司的开发环境都会做限制,只有通过golint规范检测的代码才可以提交发布。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">//&nbsp;HelloWorld&nbsp;print&nbsp;hello&nbsp;world</span><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">func</span>&nbsp;<span class="hljs-title" style="color: rgba(153, 0, 0, 1); font-weight: bold; line-height: 26px">HelloWorld</span><span class="hljs-params" style="line-height: 26px">()</span></span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"Hello&nbsp;World"</span>)<br>}<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">另外一点是golang<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">不支持隐式类型转换</strong>,比如int和int32以及int64,会被视作是不同的类型。如果我们将一个int32的变量赋值给int类型,则会引起报错,必须要我们手动转换。这当然增加了编码时候的工作,但是也避免了很多由精度不一样产生的问题。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">除了这些之外,golang当中还定义了对结构体定义以及错误处理等内容的规范。但是对于我们初学者而言,目前这些是必须要了解的,其他的内容可以等我们后续遇见了再熟悉。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">一门语言对于代码风格做了严格的规范限制<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">对于初学者而言可能是一件比较蛋疼的事情</strong>,因为要记的东西变多了,我们不仅要学会语法,还要搞清楚这些规范。但是当我们熟悉了或者是工作了之后,会发现这其实是一件好事。对于多人协作的场景而言,大家都遵守一样的规范会大大<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">提升代码交流以及协作的效率</strong>。如果你们看过其他代码风格和自己完全不同的人的代码之后,相信你们对于这点一定会有更深的认识。</p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; font-weight: bold; font-size: 22px; border-bottom: 2px solid rgba(89, 89, 89, 1); margin-bottom: 30px; color: rgba(89, 89, 89, 1)"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; display: inline-block; border-bottom: 2px solid rgba(89, 89, 89, 1)">总结</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">从规范的严格程度以及对<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">面向对象的阉割</strong>程度看起来,golang简直不像是一门新生的语言,倒有些上世纪老派语言的风格。但是偏偏golang又有很多新鲜特性,比如允许<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">函数值返回多个结果</strong>,支持匿名函数以及部分函数式编程的功能等等。在初学的阶段,我也非常抗拒它,可能是因为Python写得太多了,习惯了动态语言。但是随着对这门语言了解的深入,我越来越多地发现了它这些设计理念背后的思考和智慧,慢慢对它改观,时至今日,我已经不再怀疑这是一门优秀的语言,这几年的流行并不是没有道理的。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">另外很重要的一点是,因为golang太特立独行了,所以经常会让我思考它这么做背后的用意是什么?这么一思考,加上查阅一些资料,能够发现很多之前思维当中的盲点。在之前学习语言的时候,我是绝对不会去思考语言的设计者为什么要这么设计的,只会依葫芦画瓢,照着把相关的内容学会仅此而已。这样的思考除了能够提升对于语言本身的理解之外,也<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">能够提升对问题场景的思考和理解</strong>,对于工程师而言,后者其实是更为重要的。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">当然这些内容我光说是没有用的,也需要屏幕前的你用心去体会。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(89, 89, 89, 1)">希望大家都能感受到golang的魅力,都能在此过程当中收货成长,加油!如果觉得有所收获,请顺手点个<strong style="font-weight: bold; color: rgba(71, 193, 168, 1)">在看或者转发</strong>吧,你们的举手之劳对我来说很重要。</p>
</section>
<p><img src="https://user-gold-cdn.xitu.io/2020/4/20/171978c0b27afec0?w=258&amp;h=258&amp;f=png&amp;s=23988"></p><br><br>
来源:https://www.cnblogs.com/techflow/p/12740198.html
頁: [1]
查看完整版本: Golang——详解Go语言代码规范