文章列表


详解设计模式之代理模式-php解释

<p></p><pre class="brush:as3;toolbar:false">&lt;?php class&nbsp;SchoolGirl { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$name; &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;__construct($name) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;name&nbsp;=&nbsp;$name; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getName() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&gt;name; &nbsp;&nbsp;&nbsp;&nbsp;} } //&nbsp;代理接口 interface&nbsp;GiveGift { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;GiveDolls(); &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;GiveFlowers(); &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;GiveChocolate(); } //&nbsp;代理实现送礼物接口 class&nbsp;Proxy&nbsp;implements&nbsp;GiveGift { &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$pursuit; &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;__construct(SchoolGirl&nbsp;$girl) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;pursuit&nbsp;=&nbsp;new&nbsp;Pursuit($girl); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;GiveDolls() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;pursuit-&gt;GiveDolls(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;GiveFlowers() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;pursuit-&gt;GiveFlowers(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;GiveChocolate() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;pursuit-&gt;GiveChocolate(); &nbsp;&nbsp;&nbsp;&nbsp;} } //&nbsp;追求者类实现送礼物接口 class&nbsp;Pursuit&nbsp;implements&nbsp;GiveGift { &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$girl; &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;__construct(SchoolGirl&nbsp;$girl) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;girl&nbsp;=&nbsp;$girl; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;GiveDolls() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$this-&gt;girl-&gt;getName().&quot;&nbsp;送你娃娃\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;GiveFlowers() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$this-&gt;girl-&gt;getName().&quot;&nbsp;送你花\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;GiveChocolate() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$this-&gt;girl-&gt;getName().&quot;&nbsp;送你巧克力\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;} } //&nbsp;客户端代码 $girl&nbsp;=&nbsp;new&nbsp;SchoolGirl(&#39;李梅&#39;); $proxy&nbsp;=&nbsp;new&nbsp;Proxy($girl); $proxy-&gt;GiveDolls(); $proxy-&gt;GiveChocolate(); $proxy-&gt;GiveFlowers();</pre><p><span style="color: #6a9955;"></span><br/></p><p><br/></p>

详解设计模式之装饰器模式-php解释

<p></p><pre class="brush:as3;toolbar:false">&lt;?php abstract&nbsp;class&nbsp;Component { &nbsp;&nbsp;&nbsp;&nbsp;abstract&nbsp;public&nbsp;function&nbsp;Operation(); } /** *&nbsp;ConcreteComponent */ class&nbsp;ConcreteComponent&nbsp;extends&nbsp;Component { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;Operation() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;具体对象的操作.\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;} } abstract&nbsp;class&nbsp;Decorator&nbsp;extends&nbsp;Component { &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$component; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;设置component &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;SetComponent($component) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;component&nbsp;=&nbsp;$component; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;重写Operation(),实际执行的是component的Operation方法 &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;Operation() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($this-&gt;component&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;component-&gt;Operation(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} } /** *&nbsp;ConcreteDecoratorA */ class&nbsp;ConcreteDecoratorA&nbsp;extends&nbsp;Decorator { &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;本类的独有功能,以区别于ConcreteDecoratorB &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$addedState; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;Operation() &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;首先运行原Component的Operation(),再执行本类的功能, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;如addedState,相当于对原Component进行了装饰 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Operation(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;addedState&nbsp;=&nbsp;&quot;ConcreteDecoratorA&nbsp;Status&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$this-&gt;addedState.&quot;\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;具体装饰对象A的操作.\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;} } /** *&nbsp;ConcreteDecoratorB */ class&nbsp;ConcreteDecoratorB&nbsp;extends&nbsp;Decorator { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;Operation() &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;首先运行原Component的Operation(),再执行本类的功能, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;如addedBehavior,相当于对原Component进行了装饰 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Operation(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;addedBehavior(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;具体装饰对象B的操作.\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;本类的独有功能,以区别于ConcreteDecoratorA &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;function&nbsp;addedBehavior() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;ConcreteDecoratorB&nbsp;Status.\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;} } //&nbsp;客户端代码 //&nbsp;装饰的方法是:首先用ConcreteComponent实例化对象c, //&nbsp;然后用ConcreteDecoratorA的实例对象$di来包装$c, //&nbsp;然后再用ConcreteDecoratorB的实例$d2包装$d1, //&nbsp;最终执行$d2的Operation();&nbsp; $c&nbsp;=&nbsp;new&nbsp;ConcreteComponent(); $d1&nbsp;=&nbsp;new&nbsp;ConcreteDecoratorA(); $d2&nbsp;=&nbsp;new&nbsp;ConcreteDecoratorB(); $d1-&gt;SetComponent($c); $d2-&gt;SetComponent($d1); $d2-&gt;Operation(); 当只有一个ConcreteComponent类而没有抽象的Component类&nbsp;,那么Decorator类可以是ConcreteComponent的一个子类。同样的道理,如果只有一个ConcreteDecorator类&nbsp;,那么就没有必要建立一个单独的Decorator类,而可以把Decorator类和ConcreteComponent类的责任合并成一个类。 书中用打扮的代码阐释了这样情况下的写法: &lt;?php //&nbsp;人 class&nbsp;Person {&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$name; &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;__construct($name) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;name&nbsp;=&nbsp;$name; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;show() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;打扮&quot;.$this-&gt;name.&quot;\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;} } //&nbsp;服饰类 class&nbsp;Finery { &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$person; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;decorate($person) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;person&nbsp;=&nbsp;$person; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;show() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($this-&gt;person&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;person-&gt;show(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} } //&nbsp;具体服饰类 class&nbsp;TShirts&nbsp;extends&nbsp;Finery { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;show() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;大T恤\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::show(); &nbsp;&nbsp;&nbsp;&nbsp;} } class&nbsp;BigTrouser&nbsp;extends&nbsp;Finery { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;show() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;跨裤\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::show(); &nbsp;&nbsp;&nbsp;&nbsp;} } class&nbsp;Sneakers&nbsp;extends&nbsp;Finery { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;show() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;破球鞋\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::show(); &nbsp;&nbsp;&nbsp;&nbsp;} } class&nbsp;Suit&nbsp;extends&nbsp;Finery { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;show() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;西装\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::show(); &nbsp;&nbsp;&nbsp;&nbsp;} } class&nbsp;Tie&nbsp;extends&nbsp;Finery { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;show() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;领带\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::show(); &nbsp;&nbsp;&nbsp;&nbsp;} } class&nbsp;LeatherShoes&nbsp;extends&nbsp;Finery { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;show() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;跨裤\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::show(); &nbsp;&nbsp;&nbsp;&nbsp;} } //&nbsp;客户端代码 $person&nbsp;=&nbsp;new&nbsp;Person(&quot;alex&quot;); $sneakers&nbsp;=&nbsp;new&nbsp;Sneakers(); $bigTrouser&nbsp;=&nbsp;new&nbsp;BigTrouser(); $tShirts&nbsp;=&nbsp;new&nbsp;TShirts(); $sneakers-&gt;decorate($person); $bigTrouser-&gt;decorate($sneakers); $tShirts-&gt;decorate($bigTrouser); $tShirts-&gt;show();</pre><p><span style="color: #6a9955;"></span><br/></p><p>总结</p><p><br/></p><p>装饰模式,动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。</p><p><br/></p><p>装饰模式是为已有功能动态的添加更多功能的一种方式。</p><p><br/></p><p>当系统需要新功能的时候,是向旧的类中添加新的代码。这些新的代码通常装饰了原有类的核心职责或主要行为。</p><p><br/></p><p>在主类中加入新的字段,新的方法和新的逻辑,从而增加了主类的复杂度,而这些新加入的东西仅仅是为满足一些只在某种特定情况下才会执行的特殊行为的需要。装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它要装饰的对象,对此,当需要执行特殊行为时,客户代码就可以在运行时根据需要选择地,按顺序地使用装饰功能包装对象了。</p><p><br/></p><p>装饰模式的优点就是把类中的装饰功能从类中搬移去除,这样可以简化原有的类。</p><p><br/></p><p>有效地把类的核心职责和装饰功能区分开了,而且可以去除相关类中的重复的装饰逻辑。</p><p><br/></p>

kubernetes集群部署之kube-apiserver集群部署

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">本文档讲解部署一个三实例 kube-apiserver 集群的步骤.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">注意:如果没有特殊指明,本文档的所有操作<strong style="box-sizing: border-box;">均在 zhangjun-k8s-01 节点上执行</strong>。</p><h2 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 1em; font-weight: 300; line-height: 1.225; font-size: 1.75em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; padding-bottom: 0.5em; border-bottom: 1px solid rgb(238, 238, 238); color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>创建 kubernetes-master 证书和私钥</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">创建证书签名请求:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">cd&nbsp;/opt/k8s/worksource&nbsp;/opt/k8s/bin/environment.shcat&nbsp;&gt;&nbsp;kubernetes-csr.json&nbsp;&lt;&lt;EOF{&nbsp;&nbsp;&quot;CN&quot;:&nbsp;&quot;kubernetes-master&quot;,&nbsp;&nbsp;&quot;hosts&quot;:&nbsp;[&nbsp;&nbsp;&nbsp;&nbsp;&quot;127.0.0.1&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;172.27.138.251&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;172.27.137.229&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;172.27.138.239&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;${CLUSTER_KUBERNETES_SVC_IP}&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;kubernetes&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;kubernetes.default&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;kubernetes.default.svc&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;kubernetes.default.svc.cluster&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;kubernetes.default.svc.cluster.local.&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;kubernetes.default.svc.${CLUSTER_DNS_DOMAIN}.&quot;&nbsp;&nbsp;],&nbsp;&nbsp;&quot;key&quot;:&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&quot;algo&quot;:&nbsp;&quot;rsa&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&quot;size&quot;:&nbsp;2048&nbsp;&nbsp;},&nbsp;&nbsp;&quot;names&quot;:&nbsp;[&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;C&quot;:&nbsp;&quot;CN&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;ST&quot;:&nbsp;&quot;BeiJing&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;L&quot;:&nbsp;&quot;BeiJing&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;O&quot;:&nbsp;&quot;k8s&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;OU&quot;:&nbsp;&quot;opsnull&quot;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;]}EOF</pre><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;" class=" list-paddingleft-2"><li><p>hosts 字段指定授权使用该证书的&nbsp;<strong style="box-sizing: border-box;">IP 和域名列表</strong>,这里列出了 master 节点 IP、kubernetes 服务的 IP 和域名;</p></li></ul><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">生成证书和私钥:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">cfssl&nbsp;gencert&nbsp;-ca=/opt/k8s/work/ca.pem&nbsp;\&nbsp;&nbsp;-ca-key=/opt/k8s/work/ca-key.pem&nbsp;\&nbsp;&nbsp;-config=/opt/k8s/work/ca-config.json&nbsp;\&nbsp;&nbsp;-profile=kubernetes&nbsp;kubernetes-csr.json&nbsp;|&nbsp;cfssljson&nbsp;-bare&nbsp;kubernetesls&nbsp;kubernetes*pem</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">将生成的证书和私钥文件拷贝到所有 master 节点:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">cd&nbsp;/opt/k8s/worksource&nbsp;/opt/k8s/bin/environment.shfor&nbsp;node_ip&nbsp;in&nbsp;${NODE_IPS[@]}&nbsp;&nbsp;do&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;&gt;&gt;&gt;&nbsp;${node_ip}&quot;&nbsp;&nbsp;&nbsp;&nbsp;ssh&nbsp;root@${node_ip}&nbsp;&quot;mkdir&nbsp;-p&nbsp;/etc/kubernetes/cert&quot;&nbsp;&nbsp;&nbsp;&nbsp;scp&nbsp;kubernetes*.pem&nbsp;root@${node_ip}:/etc/kubernetes/cert/&nbsp;&nbsp;done</pre><h2 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 1em; font-weight: 300; line-height: 1.225; font-size: 1.75em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; padding-bottom: 0.5em; border-bottom: 1px solid rgb(238, 238, 238); color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>创建加密配置文件</h2><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">cd&nbsp;/opt/k8s/worksource&nbsp;/opt/k8s/bin/environment.shcat&nbsp;&gt;&nbsp;encryption-config.yaml&nbsp;&lt;&lt;EOFkind:&nbsp;EncryptionConfigapiVersion:&nbsp;v1resources:&nbsp;&nbsp;-&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;secrets&nbsp;&nbsp;&nbsp;&nbsp;providers:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;aescbc:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keys:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;name:&nbsp;key1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secret:&nbsp;${ENCRYPTION_KEY}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;identity:&nbsp;{}EOF</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">将加密配置文件拷贝到 master 节点的&nbsp;<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/etc/kubernetes</code>&nbsp;目录下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">cd&nbsp;/opt/k8s/worksource&nbsp;/opt/k8s/bin/environment.shfor&nbsp;node_ip&nbsp;in&nbsp;${NODE_IPS[@]}&nbsp;&nbsp;do&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;&gt;&gt;&gt;&nbsp;${node_ip}&quot;&nbsp;&nbsp;&nbsp;&nbsp;scp&nbsp;encryption-config.yaml&nbsp;root@${node_ip}:/etc/kubernetes/&nbsp;&nbsp;done</pre><h2 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 1em; font-weight: 300; line-height: 1.225; font-size: 1.75em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; padding-bottom: 0.5em; border-bottom: 1px solid rgb(238, 238, 238); color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>创建审计策略文件</h2><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">cd&nbsp;/opt/k8s/worksource&nbsp;/opt/k8s/bin/environment.shcat&nbsp;&gt;&nbsp;audit-policy.yaml&nbsp;&lt;&lt;EOFapiVersion:&nbsp;audit.k8s.io/v1beta1kind:&nbsp;Policyrules:&nbsp;&nbsp;#&nbsp;The&nbsp;following&nbsp;requests&nbsp;were&nbsp;manually&nbsp;identified&nbsp;as&nbsp;high-volume&nbsp;and&nbsp;low-risk,&nbsp;so&nbsp;drop&nbsp;them.&nbsp;&nbsp;-&nbsp;level:&nbsp;None&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;endpoints&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;services&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;services/status&nbsp;&nbsp;&nbsp;&nbsp;users:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:kube-proxy&#39;&nbsp;&nbsp;&nbsp;&nbsp;verbs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;watch&nbsp;&nbsp;-&nbsp;level:&nbsp;None&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;nodes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;nodes/status&nbsp;&nbsp;&nbsp;&nbsp;userGroups:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:nodes&#39;&nbsp;&nbsp;&nbsp;&nbsp;verbs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;get&nbsp;&nbsp;-&nbsp;level:&nbsp;None&nbsp;&nbsp;&nbsp;&nbsp;namespaces:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;kube-system&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;endpoints&nbsp;&nbsp;&nbsp;&nbsp;users:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:kube-controller-manager&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:kube-scheduler&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:serviceaccount:kube-system:endpoint-controller&#39;&nbsp;&nbsp;&nbsp;&nbsp;verbs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;get&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;update&nbsp;&nbsp;-&nbsp;level:&nbsp;None&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;namespaces&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;namespaces/status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;namespaces/finalize&nbsp;&nbsp;&nbsp;&nbsp;users:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:apiserver&#39;&nbsp;&nbsp;&nbsp;&nbsp;verbs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;get&nbsp;&nbsp;#&nbsp;Don&#39;t&nbsp;log&nbsp;HPA&nbsp;fetching&nbsp;metrics.&nbsp;&nbsp;-&nbsp;level:&nbsp;None&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;metrics.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;users:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:kube-controller-manager&#39;&nbsp;&nbsp;&nbsp;&nbsp;verbs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;get&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;list&nbsp;&nbsp;#&nbsp;Don&#39;t&nbsp;log&nbsp;these&nbsp;read-only&nbsp;URLs.&nbsp;&nbsp;-&nbsp;level:&nbsp;None&nbsp;&nbsp;&nbsp;&nbsp;nonResourceURLs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;/healthz*&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;/version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;/swagger*&#39;&nbsp;&nbsp;#&nbsp;Don&#39;t&nbsp;log&nbsp;events&nbsp;requests.&nbsp;&nbsp;-&nbsp;level:&nbsp;None&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;events&nbsp;&nbsp;#&nbsp;node&nbsp;and&nbsp;pod&nbsp;status&nbsp;calls&nbsp;from&nbsp;nodes&nbsp;are&nbsp;high-volume&nbsp;and&nbsp;can&nbsp;be&nbsp;large,&nbsp;don&#39;t&nbsp;log&nbsp;responses&nbsp;&nbsp;#&nbsp;for&nbsp;expected&nbsp;updates&nbsp;from&nbsp;nodes&nbsp;&nbsp;-&nbsp;level:&nbsp;Request&nbsp;&nbsp;&nbsp;&nbsp;omitStages:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;RequestReceived&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;nodes/status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;pods/status&nbsp;&nbsp;&nbsp;&nbsp;users:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;kubelet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:node-problem-detector&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:serviceaccount:kube-system:node-problem-detector&#39;&nbsp;&nbsp;&nbsp;&nbsp;verbs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;update&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;patch&nbsp;&nbsp;-&nbsp;level:&nbsp;Request&nbsp;&nbsp;&nbsp;&nbsp;omitStages:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;RequestReceived&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;nodes/status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;pods/status&nbsp;&nbsp;&nbsp;&nbsp;userGroups:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:nodes&#39;&nbsp;&nbsp;&nbsp;&nbsp;verbs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;update&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;patch&nbsp;&nbsp;#&nbsp;deletecollection&nbsp;calls&nbsp;can&nbsp;be&nbsp;large,&nbsp;don&#39;t&nbsp;log&nbsp;responses&nbsp;for&nbsp;expected&nbsp;namespace&nbsp;deletions&nbsp;&nbsp;-&nbsp;level:&nbsp;Request&nbsp;&nbsp;&nbsp;&nbsp;omitStages:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;RequestReceived&nbsp;&nbsp;&nbsp;&nbsp;users:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&#39;system:serviceaccount:kube-system:namespace-controller&#39;&nbsp;&nbsp;&nbsp;&nbsp;verbs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;deletecollection&nbsp;&nbsp;#&nbsp;Secrets,&nbsp;ConfigMaps,&nbsp;and&nbsp;TokenReviews&nbsp;can&nbsp;contain&nbsp;sensitive&nbsp;&amp;&nbsp;binary&nbsp;data,&nbsp;&nbsp;#&nbsp;so&nbsp;only&nbsp;log&nbsp;at&nbsp;the&nbsp;Metadata&nbsp;level.&nbsp;&nbsp;-&nbsp;level:&nbsp;Metadata&nbsp;&nbsp;&nbsp;&nbsp;omitStages:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;RequestReceived&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;secrets&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;configmaps&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;authentication.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;tokenreviews&nbsp;&nbsp;#&nbsp;Get&nbsp;repsonses&nbsp;can&nbsp;be&nbsp;large;&nbsp;skip&nbsp;them.&nbsp;&nbsp;-&nbsp;level:&nbsp;Request&nbsp;&nbsp;&nbsp;&nbsp;omitStages:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;RequestReceived&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;admissionregistration.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;apiextensions.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;apiregistration.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;apps&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;authentication.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;authorization.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;autoscaling&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;batch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;certificates.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;extensions&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;metrics.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;networking.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;policy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;rbac.authorization.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;scheduling.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;settings.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;storage.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;verbs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;get&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;watch&nbsp;&nbsp;#&nbsp;Default&nbsp;level&nbsp;for&nbsp;known&nbsp;APIs&nbsp;&nbsp;-&nbsp;level:&nbsp;RequestResponse&nbsp;&nbsp;&nbsp;&nbsp;omitStages:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;RequestReceived&nbsp;&nbsp;&nbsp;&nbsp;resources:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;admissionregistration.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;apiextensions.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;apiregistration.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;apps&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;authentication.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;authorization.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;autoscaling&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;batch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;certificates.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;extensions&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;metrics.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;networking.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;policy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;rbac.authorization.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;scheduling.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;settings.k8s.io&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;group:&nbsp;storage.k8s.io&nbsp;&nbsp;#&nbsp;Default&nbsp;level&nbsp;for&nbsp;all&nbsp;other&nbsp;requests.&nbsp;&nbsp;-&nbsp;level:&nbsp;Metadata&nbsp;&nbsp;&nbsp;&nbsp;omitStages:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;RequestReceivedEOF</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">分发审计策略文件:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">cd&nbsp;/opt/k8s/worksource&nbsp;/opt/k8s/bin/environment.shfor&nbsp;node_ip&nbsp;in&nbsp;${NODE_IPS[@]}&nbsp;&nbsp;do&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;&gt;&gt;&gt;&nbsp;${node_ip}&quot;&nbsp;&nbsp;&nbsp;&nbsp;scp&nbsp;audit-policy.yaml&nbsp;root@${node_ip}:/etc/kubernetes/audit-policy.yaml&nbsp;&nbsp;don</pre><p><br/></p>

kubernetes集群部署之部署master节点

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">kubernetes master 节点运行如下组件:</p><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;" class=" list-paddingleft-2"><li><p>kube-apiserver</p></li><li><p>kube-scheduler</p></li><li><p>kube-controller-manager</p></li></ul><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">kube-apiserver、kube-scheduler 和 kube-controller-manager 均以多实例模式运行:</p><ol style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;" class=" list-paddingleft-2"><li><p>kube-scheduler 和 kube-controller-manager 会自动选举产生一个 leader 实例,其它实例处于阻塞模式,当 leader 挂了后,重新选举产生新的 leader,从而保证服务可用性;</p></li><li><p>kube-apiserver 是无状态的,可以通过 kube-nginx 进行代理访问(见<a href="https://www.maxiaoke.com/manual/kuber_sz/06-2.apiserver%E9%AB%98%E5%8F%AF%E7%94%A8.md" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;">06-2.apiserver高可用</a>),从而保证服务可用性;</p></li></ol><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">注意:如果没有特殊指明,本文档的所有操作<strong style="box-sizing: border-box;">均在 zhangjun-k8s01 节点上执行</strong>。</p><h2 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 1em; font-weight: 300; line-height: 1.225; font-size: 1.75em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; padding-bottom: 0.5em; border-bottom: 1px solid rgb(238, 238, 238); color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>下载最新版本二进制文件</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">从&nbsp;<a href="https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG.md" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;">CHANGELOG 页面</a>&nbsp;下载二进制 tar 文件并解压:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">cd&nbsp;/opt/k8s/workwget&nbsp;https://dl.k8s.io/v1.16.6/kubernetes-server-linux-amd64.tar.gz&nbsp;&nbsp;#&nbsp;自行解决翻墙问题tar&nbsp;-xzvf&nbsp;kubernetes-server-linux-amd64.tar.gzcd&nbsp;kubernetestar&nbsp;-xzvf&nbsp;&nbsp;kubernetes-src.tar.gz</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">将二进制文件拷贝到所有 master 节点:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap; margin-bottom: 0px !important;">cd&nbsp;/opt/k8s/worksource&nbsp;/opt/k8s/bin/environment.shfor&nbsp;node_ip&nbsp;in&nbsp;${NODE_IPS[@]}&nbsp;&nbsp;do&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;&gt;&gt;&gt;&nbsp;${node_ip}&quot;&nbsp;&nbsp;&nbsp;&nbsp;scp&nbsp;kubernetes/server/bin/{apiextensions-apiserver,kube-apiserver,kube-controller-manager,kube-proxy,kube-scheduler,kubeadm,kubectl,kubelet,mounter}&nbsp;root@${node_ip}:/opt/k8s/bin/&nbsp;&nbsp;&nbsp;&nbsp;ssh&nbsp;root@${node_ip}&nbsp;&quot;chmod&nbsp;+x&nbsp;/opt/k8s/bin/*&quot;&nbsp;&nbsp;done</pre><p><br/></p>

go语言学习之go单元测试和性能测试

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">测试对于互联网应用软件开发来说非常重要,它对软件可靠性保证具有重要意义,通过测试能够尽可能发现并改正软件中的错误,提高软件质量。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">这里我们主要讲解Go语言如何实现单元测试和性能测试。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">go语言中自带有一个轻量级的测试框架testing和自带的go test命令来实现单元测试和性能测试,testing框架和其他语言中的测试框架类似,你可以基于这个框架写针对相应函数的测试用例,也可以基于该框架写相应的压力测试用例,那么接下来让我们一一来看一下怎么写。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><strong style="box-sizing: border-box;">单元测试</strong></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">创建目录test,在目录下创建add.go、add_test.go两个文件,add_test.go为单元测试文件。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">add_test.go</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;testimport&nbsp;&quot;testing&quot; func&nbsp;TestAdd(t&nbsp;*&nbsp;testing.T) { &nbsp;&nbsp;&nbsp;&nbsp;sum:&nbsp;=&nbsp;Add(1,&nbsp;2)&nbsp;if&nbsp;sum&nbsp;==&nbsp;3 &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Log(&quot;the&nbsp;result&nbsp;is&nbsp;ok&quot;) &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Fatal(&quot;the&nbsp;result&nbsp;is&nbsp;wrong&quot;) &nbsp;&nbsp;&nbsp;&nbsp;} } func&nbsp;TestAdd1(t&nbsp;*&nbsp;testing.T) { &nbsp;&nbsp;&nbsp;&nbsp;t.Error(&quot;the&nbsp;result&nbsp;is&nbsp;error&quot;) }</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">add.go</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;testfunc&nbsp;Add(a,&nbsp;b&nbsp;int)&nbsp;int&nbsp;{&nbsp;&nbsp;&nbsp;return&nbsp;a&nbsp;+&nbsp;b}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">然后在项目目录下运行<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">go test -v</code>就可以看到测试结果了</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">===&nbsp;RUN&nbsp;&nbsp;&nbsp;TestAdd---&nbsp;PASS:&nbsp;TestAdd&nbsp;(0.00s)&nbsp;&nbsp;&nbsp;&nbsp;add_test.go:8:&nbsp;the&nbsp;result&nbsp;is&nbsp;ok===&nbsp;RUN&nbsp;&nbsp;&nbsp;TestAdd1---&nbsp;FAIL:&nbsp;TestAdd1&nbsp;(0.00s)&nbsp;&nbsp;&nbsp;&nbsp;add_test.go:14:&nbsp;the&nbsp;result&nbsp;is&nbsp;errorFAILexit&nbsp;status&nbsp;1FAIL&nbsp;&nbsp;&nbsp;&nbsp;_/D_/gopath/src/ados/test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.419s</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">如果看到PASS字样证明测试通过,FAIL字样表示测试失败。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">使用testing库的测试框架需要遵循以下几个规则如下:</p><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;" class=" list-paddingleft-2"><li><p>文件名必须是<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">_test.go</code>结尾的,这样在执行<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">go test</code>的时候才会执行到相应的代码</p></li><li><p>你必须import&nbsp;<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">testing</code>这个包</p></li><li><p>所有的测试用例函数必须是<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">Test</code>开头</p></li><li><p>测试用例会按照源代码中写的顺序依次执行</p></li><li><p>测试函数<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">TestXxx()</code>的参数是<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">testing.T</code>,我们可以使用该类型来记录错误或者是测试状态</p></li><li><p>测试格式:<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">func TestXxx (t *testing.T)</code>,<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">Xxx</code>部分可以为任意的字母数字的组合,但是首字母不能是小写字母[a-z],例如<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">Testintdiv</code>是错误的函数名。</p></li><li><p>函数中通过调用<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">testing.T</code>的<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">Error</code>,&nbsp;<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">Errorf</code>,&nbsp;<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">FailNow</code>,&nbsp;<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">Fatal</code>,&nbsp;<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">FatalIf</code>方法,说明测试不通过,调用<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">Log</code>方法用来记录测试的信息。</p></li></ul><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><strong style="box-sizing: border-box;">性能测试或压力测试</strong></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">压力测试用来检测函数(方法)的性能,和编写单元功能测试的方法类似,此处不再赘述,但需要注意以下几点:</p><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;" class=" list-paddingleft-2"><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">压力测试用例必须遵循如下格式,其中XXX可以是任意字母数字的组合,但是首字母不能是小写字母</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;"><br/></pre></li><ol class="linenums list-paddingleft-2" style="list-style-type: lower-roman;"><li><p><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: inherit; overflow-wrap: normal; word-break: normal; padding: 0px; margin: 0px; background: none; border-radius: 3px; border: none; display: inline; max-width: initial; overflow: initial; line-height: inherit;"><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">func </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">BenchmarkXXX</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">b </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">*</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">testing</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">B</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">)</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">...</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span></code></p></li></ol><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">go test</code>不会默认执行压力测试的函数,如果要执行压力测试需要带上参数<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">-test.bench</code>,语法:<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">-test.bench=&quot;test_name_regex&quot;</code>,例如<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">go test -test.bench=&quot;.*&quot;</code>表示测试全部的压力测试函数</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">在压力测试用例中,请记得在循环体内使用<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">testing.B.N</code>,以使测试可以正常的运行</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">文件名也必须以<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">_test.go</code>结尾</p></li></ul><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在test目录下创建 reflect_test.go</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;testimport(&quot;reflect&quot; &nbsp;&nbsp;&nbsp;&nbsp;&quot;testing&quot;)&nbsp;type&nbsp;Student&nbsp;struct { &nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;string&nbsp;Age&nbsp;int&nbsp;Class&nbsp;string&nbsp;Score&nbsp;int } func&nbsp;BenchmarkReflect_New(b&nbsp;*&nbsp;testing.B) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;s&nbsp;*&nbsp;Student&nbsp;sv:&nbsp;=&nbsp;reflect.TypeOf(Student &nbsp;&nbsp;&nbsp;&nbsp;{})&nbsp;b.ResetTimer()&nbsp;for&nbsp;i:&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;&lt;&nbsp;b.N; &nbsp;&nbsp;&nbsp;&nbsp;i++ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sn:&nbsp;=&nbsp;reflect.New(sv)&nbsp;s, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;=&nbsp;sn.Interface().(&nbsp;*&nbsp;Student) &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;=&nbsp;s } func&nbsp;BenchmarkDirect_New(b&nbsp;*&nbsp;testing.B) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;s&nbsp;*&nbsp;Student&nbsp;b.ResetTimer()&nbsp;for&nbsp;i:&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;&lt;&nbsp;b.N; &nbsp;&nbsp;&nbsp;&nbsp;i++ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;new(Student) &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;=&nbsp;s } func&nbsp;BenchmarkReflect_Set(b&nbsp;*&nbsp;testing.B) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;s&nbsp;*&nbsp;Student&nbsp;sv:&nbsp;=&nbsp;reflect.TypeOf(Student &nbsp;&nbsp;&nbsp;&nbsp;{})&nbsp;b.ResetTimer()&nbsp;for&nbsp;i:&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;&lt;&nbsp;b.N; &nbsp;&nbsp;&nbsp;&nbsp;i++ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sn:&nbsp;=&nbsp;reflect.New(sv)&nbsp;s&nbsp;=&nbsp;sn.Interface().(&nbsp;*&nbsp;Student)&nbsp;s.Name&nbsp;=&nbsp;&quot;Jerry&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.Age&nbsp;=&nbsp;18&nbsp;s.Class&nbsp;=&nbsp;&quot;20005&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.Score&nbsp;=&nbsp;100 &nbsp;&nbsp;&nbsp;&nbsp;} } func&nbsp;BenchmarkReflect_SetFieldByName(b&nbsp;*&nbsp;testing.B) { &nbsp;&nbsp;&nbsp;&nbsp;sv:&nbsp;=&nbsp;reflect.TypeOf(Student &nbsp;&nbsp;&nbsp;&nbsp;{})&nbsp;b.ResetTimer()&nbsp;for&nbsp;i:&nbsp;=&nbsp;0;i&nbsp;&lt;&nbsp;b.N;i++ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sn:&nbsp;=&nbsp;reflect.New(sv).Elem()&nbsp;sn.FieldByName(&quot;Name&quot;).SetString(&quot;Jerry&quot;)&nbsp;sn.FieldByName(&quot;Age&quot;).SetInt(18)&nbsp;sn.FieldByName(&quot;Class&quot;).SetString(&quot;20005&quot;)&nbsp;sn.FieldByName(&quot;Score&quot;).SetInt(100) &nbsp;&nbsp;&nbsp;&nbsp;} } func&nbsp;BenchmarkReflect_SetFieldByIndex(b&nbsp;*&nbsp;testing.B) { &nbsp;&nbsp;&nbsp;&nbsp;sv:&nbsp;=&nbsp;reflect.TypeOf(Student &nbsp;&nbsp;&nbsp;&nbsp;{})&nbsp;b.ResetTimer()&nbsp;for&nbsp;i:&nbsp;=&nbsp;0;i&nbsp;&lt;&nbsp;b.N;i++ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sn:&nbsp;=&nbsp;reflect.New(sv).Elem()&nbsp;sn.Field(0).SetString(&quot;Jerry&quot;)&nbsp;sn.Field(1).SetInt(18)&nbsp;sn.Field(2).SetString(&quot;20005&quot;)&nbsp;sn.Field(3).SetInt(100) &nbsp;&nbsp;&nbsp;&nbsp;} } func&nbsp;BenchmarkDirect_Set(b&nbsp;*&nbsp;testing.B) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;s&nbsp;*&nbsp;Student&nbsp;b.ResetTimer()&nbsp;for&nbsp;i:&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;&lt;&nbsp;b.N; &nbsp;&nbsp;&nbsp;&nbsp;i++ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;new(Student)&nbsp;s.Name&nbsp;=&nbsp;&quot;Jerry&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.Age&nbsp;=&nbsp;18&nbsp;s.Class&nbsp;=&nbsp;&quot;20005&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.Score&nbsp;=&nbsp;100 &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在test目录下,执行:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">go test reflect_test.go -test.bench=”.*”</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">结果如下</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">goos:&nbsp;windowsgoarch:&nbsp;amd64BenchmarkReflect_New-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;84.9&nbsp;ns/opBenchmarkDirect_New-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50.6&nbsp;ns/opBenchmarkReflect_Set-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;89.9&nbsp;ns/opBenchmarkReflect_SetFieldByName-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;552&nbsp;ns/opBenchmarkReflect_SetFieldByIndex-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;132&nbsp;ns/opBenchmarkDirect_Set-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;53.0&nbsp;ns/opPASSok&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command-line-arguments&nbsp;&nbsp;10.982s</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">上面的结果显示我们没有执行任何<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">TestXXX</code>的单元测试函数,显示的结果只执行了压力测试函数,以第三行为例</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">BenchmarkReflect_New 函数执行了20000000次,每次的执行平均时间是84.9纳秒。最后一行 command-line-arguments 10.982s,代表总的执行时间为 10.982s。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">如果只想对某个函数测试,以BenchmarkReflect_New 为例,执行命令</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">go test reflect_test.go -test.bench=”BenchmarkReflect_New”</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">结果为:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">goos:&nbsp;windowsgoarch:&nbsp;amd64BenchmarkReflect_New-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;84.9&nbsp;ns/opPASSok&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command-line-arguments&nbsp;&nbsp;2.490s</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">如果测试整个目录下的所有测试执行:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">go test -test.bench=”.*”</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">如果想显示内存分配的次数和大小添加 -benchmem</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">go test reflect_test.go -benchmem -test.bench=”.*”</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">goos:&nbsp;windowsgoarch:&nbsp;amd64BenchmarkReflect_New-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20000000&nbsp;&nbsp;&nbsp;88.3&nbsp;ns/op&nbsp;&nbsp;48&nbsp;B/op&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;allocs/opBenchmarkDirect_New-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30000000&nbsp;&nbsp;&nbsp;53.8&nbsp;ns/op&nbsp;&nbsp;48&nbsp;B/op&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;allocs/opBenchmarkReflect_Set-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20000000&nbsp;&nbsp;&nbsp;90.9&nbsp;ns/op&nbsp;&nbsp;48&nbsp;B/op&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;allocs/opBenchmarkReflect_SetFieldByName-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3000000&nbsp;&nbsp;&nbsp;564&nbsp;ns/op&nbsp;&nbsp;&nbsp;80&nbsp;B/op&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;allocs/opBenchmarkReflect_SetFieldByIndex-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10000000&nbsp;&nbsp;&nbsp;135&nbsp;ns/op&nbsp;&nbsp;&nbsp;48&nbsp;B/op&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;allocs/opBenchmarkDirect_Set-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30000000&nbsp;&nbsp;&nbsp;52.4&nbsp;ns/op&nbsp;&nbsp;48&nbsp;B/op&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;allocs/opPASSok&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command-line-arguments&nbsp;&nbsp;12.955s</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">后两列代表分配的内存大小和次数(48 B/op 1 allocs/op)</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><strong style="box-sizing: border-box;">推荐<a href="https://github.com/cweill/gotests" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;">gotests</a></strong></p><p style="box-sizing: border-box; margin-top: 0px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap; margin-bottom: 0px !important;">它是编写Go测试的一个Golang命令行工具,可以根据目标源文件的函数和方法签名生成<a href="https://github.com/golang/go/wiki/TableDrivenTests" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;">表驱动的测试</a>。将自动导入测试文件中的任何新依赖项。</p><p><br/></p>

go语言学习之go性能工具PProf详解

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">Go语言自带了强大的性能测试工具pprof,位于 net/http 包下的 pprof。&nbsp;<a href="https://golang.org/pkg/net/http/pprof/" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">官方文档</a></p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a><strong style="box-sizing: border-box;">PProf 关注的模块</strong></h3><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em;" class=" list-paddingleft-2"><li><p>CPU profile:报告程序的 CPU 使用情况,按照一定频率去采集应用程序在 CPU 和寄存器上面的数据</p></li><li><p>Memory Profile(Heap Profile):报告程序的内存使用情况</p></li><li><p>Block Profiling:报告 goroutines 不在运行状态的情况,可以用来分析和查找死锁等性能瓶颈</p></li><li><p>Goroutine Profiling:报告 goroutines 的使用情况,有哪些 goroutine,它们的调用关系是怎样的</p></li></ul><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>PProf 使用方式</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">PProf 主要涉及两个包:”net/http/pprof”、”runtime/pprof”。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">net/http/pprof:只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来。使用 net/http/pprof 可以做到直接看到当前 web 服务的状态,包括 CPU 占用情况和内存使用情况等。如果服务是一直运行的,如 web 应用,可以很方便的使用第一种方式&nbsp;<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">import &quot;net/http/pprof&quot;</code>。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">runtime/pprof:可以用来产生 dump 文件,再使用 Go Tool PProf 来分析这运行日志。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">引入示例:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">#web服务器:import&nbsp;(&nbsp;&nbsp;&nbsp;&nbsp;&quot;net/http&quot;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;&quot;net/http/pprof&quot;)#一般应用程序(实际应用无web交互)import&nbsp;(&nbsp;&nbsp;&nbsp;&nbsp;&quot;net/http&quot;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;&quot;runtime/pprof&quot;)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">下面主要介绍web收集PProf信息方式</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">使用示例:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&nbsp;&quot;fmt&quot;&nbsp;&nbsp;&nbsp;&nbsp;&quot;log&quot;&nbsp;&nbsp;&nbsp;&nbsp;&quot;net/http&quot;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;&quot;net/http/pprof&quot;&nbsp;&nbsp;&nbsp;&nbsp;&quot;time&quot;)func&nbsp;sayHelloHandler(w&nbsp;http.ResponseWriter,&nbsp;r&nbsp;*http.Request)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;hellowold(10000)&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;path&quot;,&nbsp;r.URL.Path)&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;scheme&quot;,&nbsp;r.URL.Scheme)&nbsp;&nbsp;&nbsp;&nbsp;fmt.Fprintf(w,&nbsp;&quot;Hello&nbsp;world!\n&quot;)&nbsp;//这个写入到w的是输出到客户端的}func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;http.HandleFunc(&quot;/&quot;,&nbsp;sayHelloHandler)&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;设置访问路由&nbsp;&nbsp;&nbsp;&nbsp;log.Fatal(http.ListenAndServe(&quot;:8080&quot;,&nbsp;nil))}func&nbsp;hellowold(times&nbsp;int)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;time.Sleep(time.Second)&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;counter&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;:=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;times;&nbsp;i++&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;j&nbsp;:=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;times;&nbsp;j++&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counter++&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;}}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">访问:<a href="http://127.0.0.1:8080/debug/pprof/" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://127.0.0.1:8080/debug/pprof/</a></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">输出:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">/debug/pprof/profiles:0&nbsp;&nbsp;&nbsp;&nbsp;block4&nbsp;&nbsp;&nbsp;&nbsp;goroutine2&nbsp;&nbsp;&nbsp;&nbsp;heap0&nbsp;&nbsp;&nbsp;&nbsp;mutex5&nbsp;&nbsp;&nbsp;&nbsp;threadcreatefull&nbsp;goroutine&nbsp;stack&nbsp;dump</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">这个路径下还有几个子页面:</p><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em;" class=" list-paddingleft-2"><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/profile</code>:访问这个链接会自动进行 CPU profiling,持续 30s,并生成一个文件供下载。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/cmdline</code>&nbsp;获取程序启动时的命令及参数。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/symbol</code>&nbsp;根据传入的程序计数器(PC)的值,获取对应的函数的名称信息,调用了runtime包的FuncForPC获取对应的函数信息。可以传入多个PC值,以加号作为连接符号,比如访问:<a href="http://127.0.0.1:8080/debug/pprof/symbol?0x4e667d+0x6ec770" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://127.0.0.1:8080/debug/pprof/symbol?0x4e667d+0x6ec770</a>, 返回PC值与函数对应名称的信息。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/trace</code>&nbsp;此接口用于获取程序执行中的事件跟踪信息,比如协程、系统调用、GC、堆大小改变等事件,大多数事件的跟踪精确度能达到纳秒级别,后端调用了runtime包的StartTrace,会进行一个STW的操作。获取数据的时长默认为1秒,可以通过seconds参数进行修改。先通过以下命令获取数据:</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">wget&nbsp;<a href="http://127.0.0.1:8080/debug/pprof/trace?seconds=10" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://127.0.0.1:8080/debug/pprof/trace?seconds=10</a>&nbsp;-O tracefile</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">然后执行go tool trace tacefile进行数据分析。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/block</code>:block Profiling 的路径。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/goroutines</code>:运行的 goroutines 列表,以及调用关系。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/heap</code>: Memory Profiling 的路径,访问这个链接会得到一个内存 Profiling 结果的文件。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/threadcreate</code>查看线程创建信息。</p></li></ul><h4 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.4; font-size: 1.25em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>Go tool pprof 分析工具(图形工具graphviz为例)</h4><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">Go Tool PProf 工具可以对以上链接下载的 prof 文件进行更详细的分析,可以生成调用关系图和火焰图。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">centos 7.5 下安装graphviz</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">yum&nbsp;install&nbsp;&#39;graphviz*&#39;&nbsp;&nbsp;--skip-broken</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">—skip-broken可选:跳过错误依赖,不加这个参数会提示安装包依赖错误,因为这里并不需要其它的安装包,所以跳过即可。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">其他系统安装方式可参考:<a href="http://www.graphviz.org/download/" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">官网</a></p><h4 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.4; font-size: 1.25em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>Go tool pprof常用基本调试基本命令(默认30s采集时间,可通过—seconds)</h4><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">下面以cpu信息分析为例:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">通过执行</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">go&nbsp;tool&nbsp;pprof&nbsp;--text&nbsp;http://127.0.0.1:8080/debug/pprof/profile</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">生成profile压缩gz文件(一般在/root/pprof目录下的.gz),选择文件进入交互,或执行一下命令(注意:执行的过程,访问一下<a href="http://localhost:8080/%EF%BC%8C%E8%BF%99%E6%A0%B7%E4%BC%9A%E5%BE%97%E5%88%B0%E7%9B%B8%E5%BA%94%E7%9A%84%E7%9B%91%E6%B5%8B%E6%95%B0%E6%8D%AE%EF%BC%89" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://localhost:8080/,这样会得到相应的监测数据)</a></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">go&nbsp;tool&nbsp;pprof&nbsp;http://localhost:8080/debug/pprof/profileFetching&nbsp;profile&nbsp;over&nbsp;HTTP&nbsp;from&nbsp;http://localhost:8080/debug/pprof/profileSaved&nbsp;profile&nbsp;in&nbsp;/root/pprof/pprof.main.samples.cpu.002.pb.gzFile:&nbsp;mainType:&nbsp;cpuTime:&nbsp;Dec&nbsp;14,&nbsp;2018&nbsp;at&nbsp;3:26pm&nbsp;(CST)Duration:&nbsp;30s,&nbsp;Total&nbsp;samples&nbsp;=&nbsp;60ms&nbsp;(&nbsp;&nbsp;0.2%)Entering&nbsp;interactive&nbsp;mode&nbsp;(type&nbsp;&quot;help&quot;&nbsp;for&nbsp;commands,&nbsp;&quot;o&quot;&nbsp;for&nbsp;options)(pprof)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">输入top</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">(pprof)&nbsp;topShowing&nbsp;nodes&nbsp;accounting&nbsp;for&nbsp;60ms,&nbsp;100%&nbsp;of&nbsp;60ms&nbsp;total&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flat&nbsp;&nbsp;flat%&nbsp;&nbsp;&nbsp;sum%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cum&nbsp;&nbsp;&nbsp;cum%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;60ms&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;60ms&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;main.hellowold&nbsp;/home/go/src/example/main.go&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0%&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;60ms&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;main.sayHelloHandler&nbsp;/home/go/src/example/main.go&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0%&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;60ms&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;net/http.(*ServeMux).ServeHTTP&nbsp;/usr/lib/golang/src/net/http/server.go&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0%&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;60ms&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;net/http.(*conn).serve&nbsp;/usr/lib/golang/src/net/http/server.go&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0%&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;60ms&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;net/http.HandlerFunc.ServeHTTP&nbsp;/usr/lib/golang/src/net/http/server.go&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0%&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;60ms&nbsp;&nbsp;&nbsp;100%&nbsp;&nbsp;net/http.serverHandler.ServeHTTP&nbsp;/usr/lib/golang/src/net/http/server.go(pprof)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">可以看到 main.hellowold 是用cpu最多。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">输入 web #生成调用关系图</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><img src="https://www.maxiaoke.com/uploads/images/20231219/ccc573df4f6394fee008e185fef7e1af.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">如查看历史调试文件信息,通过指定的profile文件进入(pprof)即可:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">例如:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">go tool pprof /root/pprof/pprof.main.samples.cpu.001.pb.gz<br/>File: main<br/>Type: cpu<br/>Time: Dec 14, 2018 at 11:37am (CST)<br/>Duration: 29.99s, Total samples = 0<br/>Entering interactive mode (type “help” for commands, “o” for options)<br/>(pprof)</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">如果想查看pprof其他命令,可输入help。</p><h2 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 1em; font-weight: 300; line-height: 1.225; font-size: 1.75em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; padding-bottom: 0.5em; border-bottom: 1px solid rgb(238, 238, 238);"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a><strong style="box-sizing: border-box;">生成火焰图</strong>&nbsp;(官方:<a href="https://github.com/uber/go-torch%EF%BC%89" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">https://github.com/uber/go-torch)</a></h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">从Go 1.11开始,火焰图形可视化<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">go tool pprof</code>直接可用!</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">#&nbsp;This&nbsp;will&nbsp;listen&nbsp;on&nbsp;:8081&nbsp;and&nbsp;open&nbsp;a&nbsp;browser.#&nbsp;Change&nbsp;:8081&nbsp;to&nbsp;a&nbsp;port&nbsp;of&nbsp;your&nbsp;choice.$&nbsp;go&nbsp;tool&nbsp;pprof&nbsp;-http=&quot;:8081&quot;&nbsp;[binary]&nbsp;[profile]</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">如果你不能使用Go 1.11,你可以获得最新的<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">pprof</code>工具并使用它:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">#&nbsp;Get&nbsp;the&nbsp;pprof&nbsp;tool&nbsp;directly$&nbsp;go&nbsp;get&nbsp;-u&nbsp;github.com/google/pprof$&nbsp;pprof&nbsp;-http=&quot;:8081&quot;&nbsp;[binary]&nbsp;[profile]</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">我的golang 版本 v 1.11,所以只执行以下命令:(注意:在执行以下命令过程,访问一下<a href="http://localhost:8080/%EF%BC%8C%E6%B6%88%E8%80%97%E4%BB%A5%E4%B8%8B%E6%80%A7%E8%83%BD%EF%BC%89" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://localhost:8080/,消耗以下性能)</a></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">go&nbsp;tool&nbsp;pprof&nbsp;-http=&quot;:8081&quot;&nbsp;http://localhost:8080/debug/pprof/profileFetching&nbsp;profile&nbsp;over&nbsp;HTTP&nbsp;from&nbsp;http://localhost:8080/debug/pprof/profileSaved&nbsp;profile&nbsp;in&nbsp;/root/pprof/pprof.main.samples.cpu.007.pb.gz</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">过一会儿会产生个web窗口</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><img src="https://www.maxiaoke.com/uploads/images/20231219/ccefa2f92eb454206709ee9752ca1b4e.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">选择 VIEW-&gt;Flame Graph 得到火焰图形</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px !important;"><img src="https://www.maxiaoke.com/uploads/images/20231219/9455da423a193850dc38c55ebdaf1a19.png" alt=""/></p><p>上</p><p><br/></p>

go语言学习之go处理文件详解

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">文件的读写是编程语言的常见操作之一,这里讲一些Goang 读取文件的相关操作。</p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>读取文件</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">读取文件有三种方式:</p><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;" class=" list-paddingleft-2"><li><p>将文件整个读入内存</p></li><li><p>按字节数读取</p></li><li><p>按行读取</p></li></ul><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">具体实现如下:</p><h6 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.4; font-size: 1em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(119, 119, 119); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>1、将文件整个读入内存</h6><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;os&quot;&nbsp;&nbsp;&nbsp;&quot;io/ioutil&quot;&nbsp;&nbsp;&nbsp;&quot;fmt&quot;)func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;file,&nbsp;err&nbsp;:=&nbsp;os.Open(&quot;D:/gopath/src/golang_development_notes/example/log.txt&quot;)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;file.Close()&nbsp;&nbsp;&nbsp;content,&nbsp;err&nbsp;:=&nbsp;ioutil.ReadAll(file)&nbsp;&nbsp;&nbsp;fmt.Println(string(content))}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">或者</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;os&quot;&nbsp;&nbsp;&nbsp;&quot;io/ioutil&quot;&nbsp;&nbsp;&nbsp;&quot;fmt&quot;)func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;filepath&nbsp;:=&nbsp;&quot;D:/gopath/src/golang_development_notes/example/log.txt&quot;&nbsp;&nbsp;&nbsp;content&nbsp;,err&nbsp;:=ioutil.ReadFile(filepath)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;fmt.Println(string(content))}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">将文件整个读入内存,效率比较高,占用内存也最高。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><strong style="box-sizing: border-box;">2、按字节读取文件</strong></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;bufio&quot;&nbsp;&nbsp;&nbsp;&quot;fmt&quot;&nbsp;&nbsp;&nbsp;&quot;io&quot;&nbsp;&nbsp;&nbsp;&quot;io/ioutil&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;)func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;filepath&nbsp;:=&nbsp;&quot;D:/gopath/src/golang_development_notes/example/log.txt&quot;&nbsp;&nbsp;&nbsp;fi,&nbsp;err&nbsp;:=&nbsp;os.Open(filepath)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;fi.Close()&nbsp;&nbsp;&nbsp;r&nbsp;:=&nbsp;bufio.NewReader(fi)&nbsp;&nbsp;&nbsp;chunks&nbsp;:=&nbsp;make([]byte,&nbsp;0)&nbsp;&nbsp;&nbsp;buf&nbsp;:=&nbsp;make([]byte,&nbsp;1024)&nbsp;//一次读取多少个字节&nbsp;&nbsp;&nbsp;for&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n,&nbsp;err&nbsp;:=&nbsp;r.Read(buf)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;&amp;&amp;&nbsp;err&nbsp;!=&nbsp;io.EOF&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(string(buf[:n]))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;0&nbsp;==&nbsp;n&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chunks&nbsp;=&nbsp;append(chunks,&nbsp;buf[:n]...)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;fmt.Println(string(chunks))}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">或</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;fmt&quot;&nbsp;&nbsp;&nbsp;&quot;io&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;)func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;file&nbsp;:=&nbsp;&quot;D:/gopath/src/golang_development_notes/example/log.txt&quot;&nbsp;&nbsp;&nbsp;f,&nbsp;err&nbsp;:=&nbsp;os.Open(file)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;f.Close()&nbsp;&nbsp;&nbsp;chunks&nbsp;:=&nbsp;make([]byte,&nbsp;0)&nbsp;&nbsp;&nbsp;buf&nbsp;:=&nbsp;make([]byte,&nbsp;1024)&nbsp;&nbsp;&nbsp;for&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n,&nbsp;err&nbsp;:=&nbsp;f.Read(buf)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;&amp;&amp;&nbsp;err&nbsp;!=&nbsp;io.EOF&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;0&nbsp;==&nbsp;n&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chunks&nbsp;=&nbsp;append(chunks,&nbsp;buf[:n]...)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;fmt.Println(string(chunks))}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><strong style="box-sizing: border-box;">3、按行读取</strong></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;bufio&quot;&nbsp;&nbsp;&nbsp;&quot;fmt&quot;&nbsp;&nbsp;&nbsp;&quot;io&quot;&nbsp;&nbsp;&nbsp;&quot;io/ioutil&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;&nbsp;&nbsp;&nbsp;&quot;strings&quot;)func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;filepath&nbsp;:=&nbsp;&quot;D:/gopath/src/golang_development_notes/example/log.txt&quot;&nbsp;&nbsp;&nbsp;file,&nbsp;err&nbsp;:=&nbsp;os.OpenFile(filepath,&nbsp;os.O_RDWR,&nbsp;0666)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;Open&nbsp;file&nbsp;error!&quot;,&nbsp;err)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;file.Close()&nbsp;&nbsp;&nbsp;stat,&nbsp;err&nbsp;:=&nbsp;file.Stat()&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;var&nbsp;size&nbsp;=&nbsp;stat.Size()&nbsp;&nbsp;&nbsp;fmt.Println(&quot;file&nbsp;size=&quot;,&nbsp;size)&nbsp;&nbsp;&nbsp;buf&nbsp;:=&nbsp;bufio.NewReader(file)&nbsp;&nbsp;&nbsp;for&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line,&nbsp;err&nbsp;:=&nbsp;buf.ReadString(&#39;\n&#39;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;=&nbsp;strings.TrimSpace(line)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(line)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;==&nbsp;io.EOF&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;File&nbsp;read&nbsp;ok!&quot;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;Read&nbsp;file&nbsp;error!&quot;,&nbsp;err)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;}}</pre><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>写入文件</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">有以下写入方式</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><strong style="box-sizing: border-box;">1、ioutil.WriteFile</strong></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;io/ioutil&quot;)func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;content&nbsp;:=&nbsp;[]byte(&quot;测试1\n测试2\n&quot;)&nbsp;&nbsp;&nbsp;err&nbsp;:=&nbsp;ioutil.WriteFile(&quot;test.txt&quot;,&nbsp;content,&nbsp;0644)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">这种方式每次都会覆盖 test.txt内容,如果test.txt文件不存在会创建。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><strong style="box-sizing: border-box;">2、os</strong></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;fmt&quot;&nbsp;&nbsp;&nbsp;&quot;io&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;)func&nbsp;checkFileIsExist(filename&nbsp;string)&nbsp;bool&nbsp;{&nbsp;&nbsp;&nbsp;if&nbsp;_,&nbsp;err&nbsp;:=&nbsp;os.Stat(filename);&nbsp;os.IsNotExist(err)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;return&nbsp;true}func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;var&nbsp;wireteString&nbsp;=&nbsp;&quot;测试1\n测试2\n&quot;&nbsp;&nbsp;&nbsp;var&nbsp;filename&nbsp;=&nbsp;&quot;./test.txt&quot;&nbsp;&nbsp;&nbsp;var&nbsp;f&nbsp;*os.File&nbsp;&nbsp;&nbsp;var&nbsp;err1&nbsp;error&nbsp;&nbsp;&nbsp;if&nbsp;checkFileIsExist(filename)&nbsp;{&nbsp;//如果文件存在&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f,&nbsp;err1&nbsp;=&nbsp;os.OpenFile(filename,&nbsp;os.O_APPEND,&nbsp;0666)&nbsp;//打开文件&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;文件存在&quot;)&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f,&nbsp;err1&nbsp;=&nbsp;os.Create(filename)&nbsp;//创建文件&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;文件不存在&quot;)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;f.Close()&nbsp;&nbsp;&nbsp;n,&nbsp;err1&nbsp;:=&nbsp;io.WriteString(f,&nbsp;wireteString)&nbsp;//写入文件(字符串)&nbsp;&nbsp;&nbsp;if&nbsp;err1&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err1)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;fmt.Printf(&quot;写入&nbsp;%d&nbsp;个字节n&quot;,&nbsp;n)}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">此种方法可以在文件内容末尾添加新内容。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><strong style="box-sizing: border-box;">3、</strong></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;fmt&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;)func&nbsp;checkFileIsExist(filename&nbsp;string)&nbsp;bool&nbsp;{&nbsp;&nbsp;&nbsp;if&nbsp;_,&nbsp;err&nbsp;:=&nbsp;os.Stat(filename);&nbsp;os.IsNotExist(err)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;return&nbsp;true}func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;var&nbsp;str&nbsp;=&nbsp;&quot;测试1\n测试2\n&quot;&nbsp;&nbsp;&nbsp;var&nbsp;filename&nbsp;=&nbsp;&quot;./test.txt&quot;&nbsp;&nbsp;&nbsp;var&nbsp;f&nbsp;*os.File&nbsp;&nbsp;&nbsp;var&nbsp;err1&nbsp;error&nbsp;&nbsp;&nbsp;if&nbsp;checkFileIsExist(filename)&nbsp;{&nbsp;//如果文件存在&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f,&nbsp;err1&nbsp;=&nbsp;os.OpenFile(filename,&nbsp;os.O_APPEND,&nbsp;0666)&nbsp;//打开文件&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;文件存在&quot;)&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f,&nbsp;err1&nbsp;=&nbsp;os.Create(filename)&nbsp;//创建文件&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;文件不存在&quot;)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;f.Close()&nbsp;&nbsp;&nbsp;n,&nbsp;err1&nbsp;:=&nbsp;f.Write([]byte(str))&nbsp;//写入文件(字节数组)&nbsp;&nbsp;&nbsp;fmt.Printf(&quot;写入&nbsp;%d&nbsp;个字节n&quot;,&nbsp;n)&nbsp;&nbsp;&nbsp;n,&nbsp;err1&nbsp;=&nbsp;f.WriteString(str)&nbsp;//写入文件(字符串)&nbsp;&nbsp;&nbsp;if&nbsp;err1&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err1)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;fmt.Printf(&quot;写入&nbsp;%d&nbsp;个字节n&quot;,&nbsp;n)&nbsp;&nbsp;&nbsp;f.Sync()}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">此种方法可以在文件内容末尾添加新内容。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><strong style="box-sizing: border-box;">4、bufio</strong></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;bufio&quot;&nbsp;&nbsp;&nbsp;&quot;fmt&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;)func&nbsp;checkFileIsExist(filename&nbsp;string)&nbsp;bool&nbsp;{&nbsp;&nbsp;&nbsp;if&nbsp;_,&nbsp;err&nbsp;:=&nbsp;os.Stat(filename);&nbsp;os.IsNotExist(err)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;return&nbsp;true}func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;var&nbsp;str&nbsp;=&nbsp;&quot;测试1\n测试2\n&quot;&nbsp;&nbsp;&nbsp;var&nbsp;filename&nbsp;=&nbsp;&quot;./test.txt&quot;&nbsp;&nbsp;&nbsp;var&nbsp;f&nbsp;*os.File&nbsp;&nbsp;&nbsp;var&nbsp;err1&nbsp;error&nbsp;&nbsp;&nbsp;if&nbsp;checkFileIsExist(filename)&nbsp;{&nbsp;//如果文件存在&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f,&nbsp;err1&nbsp;=&nbsp;os.OpenFile(filename,&nbsp;os.O_APPEND,&nbsp;0666)&nbsp;//打开文件&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;文件存在&quot;)&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f,&nbsp;err1&nbsp;=&nbsp;os.Create(filename)&nbsp;//创建文件&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;文件不存在&quot;)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;f.Close()&nbsp;&nbsp;&nbsp;if&nbsp;err1&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err1)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;w&nbsp;:=&nbsp;bufio.NewWriter(f)&nbsp;//创建新的&nbsp;Writer&nbsp;对象&nbsp;&nbsp;&nbsp;n,&nbsp;_&nbsp;:=&nbsp;w.WriteString(str)&nbsp;&nbsp;&nbsp;fmt.Printf(&quot;写入&nbsp;%d&nbsp;个字节n&quot;,&nbsp;n)&nbsp;&nbsp;&nbsp;w.Flush()}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">此种方法可以在文件内容末尾添加新内容。</p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>对比几种读取文件效率</h3><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;bufio&quot;&nbsp;&nbsp;&nbsp;&quot;fmt&quot;&nbsp;&nbsp;&nbsp;&quot;io&quot;&nbsp;&nbsp;&nbsp;&quot;io/ioutil&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;&nbsp;&nbsp;&nbsp;&quot;time&quot;)func&nbsp;read0(path&nbsp;string)&nbsp;string&nbsp;{&nbsp;&nbsp;&nbsp;file,&nbsp;err&nbsp;:=&nbsp;os.Open(path)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;file.Close()&nbsp;&nbsp;&nbsp;content,&nbsp;err&nbsp;:=&nbsp;ioutil.ReadAll(file)&nbsp;&nbsp;&nbsp;return&nbsp;string(content)}func&nbsp;read1(path&nbsp;string)&nbsp;string&nbsp;{&nbsp;&nbsp;&nbsp;content,&nbsp;err&nbsp;:=&nbsp;ioutil.ReadFile(path)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;return&nbsp;string(content)}func&nbsp;read2(path&nbsp;string)&nbsp;string&nbsp;{&nbsp;&nbsp;&nbsp;fi,&nbsp;err&nbsp;:=&nbsp;os.Open(path)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;fi.Close()&nbsp;&nbsp;&nbsp;r&nbsp;:=&nbsp;bufio.NewReader(fi)&nbsp;&nbsp;&nbsp;chunks&nbsp;:=&nbsp;make([]byte,&nbsp;0)&nbsp;&nbsp;&nbsp;buf&nbsp;:=&nbsp;make([]byte,&nbsp;1024)&nbsp;//一次读取多少个字节&nbsp;&nbsp;&nbsp;for&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n,&nbsp;err&nbsp;:=&nbsp;r.Read(buf)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;&amp;&amp;&nbsp;err&nbsp;!=&nbsp;io.EOF&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;0&nbsp;==&nbsp;n&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chunks&nbsp;=&nbsp;append(chunks,&nbsp;buf[:n]...)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;return&nbsp;string(chunks)}func&nbsp;read3(path&nbsp;string)&nbsp;string&nbsp;{&nbsp;&nbsp;&nbsp;fi,&nbsp;err&nbsp;:=&nbsp;os.Open(path)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;defer&nbsp;fi.Close()&nbsp;&nbsp;&nbsp;chunks&nbsp;:=&nbsp;make([]byte,&nbsp;0)&nbsp;&nbsp;&nbsp;buf&nbsp;:=&nbsp;make([]byte,&nbsp;1024)&nbsp;&nbsp;&nbsp;for&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n,&nbsp;err&nbsp;:=&nbsp;fi.Read(buf)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;&amp;&amp;&nbsp;err&nbsp;!=&nbsp;io.EOF&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;panic(err)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;0&nbsp;==&nbsp;n&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chunks&nbsp;=&nbsp;append(chunks,&nbsp;buf[:n]...)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;return&nbsp;string(chunks)}func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;file&nbsp;:=&nbsp;&quot;D:/gopath/src/example/example/log.txt&quot;&nbsp;&nbsp;&nbsp;start&nbsp;:=&nbsp;time.Now()&nbsp;&nbsp;&nbsp;read0(file)&nbsp;&nbsp;&nbsp;t0&nbsp;:=&nbsp;time.Now()&nbsp;&nbsp;&nbsp;fmt.Printf(&quot;Cost&nbsp;time&nbsp;%v\n&quot;,&nbsp;t0.Sub(start))&nbsp;&nbsp;&nbsp;read1(file)&nbsp;&nbsp;&nbsp;t1&nbsp;:=&nbsp;time.Now()&nbsp;&nbsp;&nbsp;fmt.Printf(&quot;Cost&nbsp;time&nbsp;%v\n&quot;,&nbsp;t1.Sub(t0))&nbsp;&nbsp;&nbsp;read2(file)&nbsp;&nbsp;&nbsp;t2&nbsp;:=&nbsp;time.Now()&nbsp;&nbsp;&nbsp;fmt.Printf(&quot;Cost&nbsp;time&nbsp;%v\n&quot;,&nbsp;t2.Sub(t1))&nbsp;&nbsp;&nbsp;read3(file)&nbsp;&nbsp;&nbsp;t3&nbsp;:=&nbsp;time.Now()&nbsp;&nbsp;&nbsp;fmt.Printf(&quot;Cost&nbsp;time&nbsp;%v\n&quot;,&nbsp;t3.Sub(t2))}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">运行结果:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">第一次</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">Cost time 6.0003ms<br/>Cost time 3.0002ms<br/>Cost time 7.0004ms<br/>Cost time 11.0006ms</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">第二次</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">Cost time 7.0004ms<br/>Cost time 4.0003ms<br/>Cost time 6.0003ms<br/>Cost time 8.0005ms</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">第三次</p><p style="box-sizing: border-box; margin-top: 0px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap; margin-bottom: 0px !important;">Cost time 9.0006ms<br/>Cost time 3.0001ms<br/>Cost time 7.0004ms<br/>Cost time 11.0007ms</p><p><br/></p>

go语言学习之go日志详解

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">golang 的log包使用起来挺简单,这里做一些简单介绍。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">示例:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;log&quot;)func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;arr&nbsp;:=&nbsp;[]int{1,&nbsp;2}&nbsp;&nbsp;&nbsp;log.Print(&quot;Print&nbsp;array&nbsp;&quot;,&nbsp;arr,&nbsp;&quot;\n&quot;)&nbsp;&nbsp;&nbsp;log.Println(&quot;Println&nbsp;array&quot;,&nbsp;arr)&nbsp;&nbsp;&nbsp;log.Printf(&quot;Printf&nbsp;array&nbsp;with&nbsp;item&nbsp;[%d,%d]\n&quot;,&nbsp;arr[0],&nbsp;arr[1])}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">结果:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">2018/12/14 18:42:02 Print array [1 2]<br/>2018/12/14 18:42:02 Println array [1 2]<br/>2018/12/14 18:42:02 Printf array with item [1,2]</p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>log.Fatal 、log.Fatalln、log.Fatalf</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">示例:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">&nbsp;&nbsp;&nbsp;&nbsp;log.Fatal(&quot;Fatal&nbsp;array&nbsp;&quot;,&nbsp;arr,&nbsp;&quot;\n&quot;)&nbsp;&nbsp;&nbsp;&nbsp;log.Fatalln(&quot;Fatalln&nbsp;array&quot;,&nbsp;arr)&nbsp;&nbsp;&nbsp;&nbsp;log.Fatalf(&quot;Fatalf&nbsp;array&nbsp;with&nbsp;item&nbsp;[%d,%d]\n&quot;,&nbsp;arr[0],&nbsp;arr[1])</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">对于 log.Fatal 接口,会先将日志内容打印到标准输出,接着调用系统的&nbsp;<strong style="box-sizing: border-box;">os.exit(1)</strong>&nbsp;接口退出程序并返回状态 1 。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在实际开发中要<strong style="box-sizing: border-box;">慎重</strong>,它导致整个系统退出,且不执行defer。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">示例</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;fmt&quot;&nbsp;&nbsp;&nbsp;&quot;log&quot;&nbsp;&nbsp;&nbsp;&quot;net/http&quot;)func&nbsp;sayHelloHandler(w&nbsp;http.ResponseWriter,&nbsp;r&nbsp;*http.Request)&nbsp;{&nbsp;&nbsp;&nbsp;defer&nbsp;func()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Println(&quot;--first--&quot;)&nbsp;&nbsp;&nbsp;}()&nbsp;&nbsp;&nbsp;log.Fatalln(&quot;test&nbsp;for&nbsp;defer&nbsp;Fatal&quot;)}func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;http.HandleFunc(&quot;/&quot;,&nbsp;sayHelloHandler)&nbsp;//&nbsp;&nbsp;&nbsp;设置访问路由&nbsp;&nbsp;&nbsp;log.Fatal(http.ListenAndServe(&quot;:8080&quot;,&nbsp;nil))}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">当访问&nbsp;<a href="http://127.0.0.1:8080/" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;">http://127.0.0.1:8080/</a>&nbsp;后,http 服务停止了,且defer没有调用。</p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>log.SetOutput</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">将日志写到指定文件中</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">示例</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;log&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;)func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;//&nbsp;按照所需读写权限创建文件&nbsp;&nbsp;&nbsp;f,&nbsp;err&nbsp;:=&nbsp;os.OpenFile(&quot;filename.log&quot;,&nbsp;os.O_WRONLY|os.O_CREATE|os.O_APPEND,&nbsp;0644)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.Fatal(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;//&nbsp;完成后延迟关闭&nbsp;&nbsp;&nbsp;defer&nbsp;f.Close()&nbsp;&nbsp;&nbsp;//设置日志输出到&nbsp;f&nbsp;&nbsp;&nbsp;log.SetOutput(f)&nbsp;&nbsp;&nbsp;//写入日志内容&nbsp;&nbsp;&nbsp;log.Println(&quot;check&nbsp;to&nbsp;make&nbsp;sure&nbsp;it&nbsp;works&quot;)}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">查看生成日志文件filename.log 内容</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">2018/12/14&nbsp;18:56:55&nbsp;check&nbsp;to&nbsp;make&nbsp;sure&nbsp;it&nbsp;works</pre><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>log.Logger、log.New</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">先自定义Logger类型, log.Logger提供了一个New方法用来创建对象:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">func&nbsp;New(out&nbsp;io.Writer,&nbsp;prefix&nbsp;string,&nbsp;flag&nbsp;int)&nbsp;*Logger</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">该函数一共有三个参数:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">(1)输出位置out,是一个io.Writer对象,该对象可以是一个文件也可以是实现了该接口的对象。通常我们可以用这个来指定日志输出到哪个文件。<br/>(2)prefix 我们在前面已经看到,就是在日志内容前面的东西。我们可以将其置为 “[Info]” 、 “[Warning]”等来帮助区分日志级别。<br/>(3) flags 是一个选项,显示日志开头的东西,可选的值有:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">Ldate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;1&nbsp;&lt;&lt;&nbsp;iota&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;形如&nbsp;2009/01/23&nbsp;的日期Ltime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;形如&nbsp;01:23:23&nbsp;&nbsp;&nbsp;的时间Lmicroseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;形如&nbsp;01:23:23.123123&nbsp;&nbsp;&nbsp;的时间Llongfile&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;全路径文件名和行号:&nbsp;/a/b/c/d.go:23&nbsp;Lshortfile&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;文件名和行号:&nbsp;d.go:23LstdFlags&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;Ldate&nbsp;|&nbsp;Ltime&nbsp;//&nbsp;日期和时间</pre><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;io&quot;&nbsp;&nbsp;&nbsp;&quot;io/ioutil&quot;&nbsp;&nbsp;&nbsp;&quot;log&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;)var&nbsp;(&nbsp;&nbsp;&nbsp;Trace&nbsp;&nbsp;&nbsp;*log.Logger&nbsp;//&nbsp;记录所有日志&nbsp;&nbsp;&nbsp;Info&nbsp;&nbsp;&nbsp;&nbsp;*log.Logger&nbsp;//&nbsp;重要的信息&nbsp;&nbsp;&nbsp;Warning&nbsp;*log.Logger&nbsp;//&nbsp;需要注意的信息&nbsp;&nbsp;&nbsp;Error&nbsp;&nbsp;&nbsp;*log.Logger&nbsp;//&nbsp;致命错误)func&nbsp;init()&nbsp;{&nbsp;&nbsp;&nbsp;file,&nbsp;err&nbsp;:=&nbsp;os.OpenFile(&quot;filename.log&quot;,&nbsp;os.O_CREATE|os.O_WRONLY|os.O_APPEND,&nbsp;0666)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;!=&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.Fatalln(err)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;Trace&nbsp;=&nbsp;log.New(ioutil.Discard,&nbsp;&quot;TRACE:&nbsp;&quot;,&nbsp;log.Ltime|log.Lshortfile)&nbsp;&nbsp;&nbsp;Info&nbsp;=&nbsp;log.New(os.Stdout,&nbsp;&quot;Info:&nbsp;&quot;,&nbsp;log.Ltime|log.Lshortfile)&nbsp;&nbsp;&nbsp;Warning&nbsp;=&nbsp;log.New(os.Stdout,&nbsp;&quot;Warning:&nbsp;&quot;,&nbsp;log.Ltime|log.Lshortfile)&nbsp;&nbsp;&nbsp;Error&nbsp;=&nbsp;log.New(io.MultiWriter(file,&nbsp;os.Stderr),&nbsp;&quot;Error&quot;,&nbsp;log.Ltime|log.Lshortfile)}func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;Trace.Println(&quot;trace&quot;)&nbsp;&nbsp;&nbsp;Info.Println(&quot;info&quot;)&nbsp;&nbsp;&nbsp;Warning.Println(&quot;warning&quot;)&nbsp;&nbsp;&nbsp;Error.Println(&quot;Error&quot;)}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">细心的朋友可能发现执行多次程序输出日志的顺序会有变化,这个主要是 os.Stderr和os.Stdout输出的不同引起的,这两个虽然都是输出到终端,但在默认情况下,stdout是行缓冲的,它的输出会放在一个buffer里面,只有到换行的时候,才会输出到屏幕。而stderr是无缓冲的,会直接输出的。参考:<a href="http://blog.sina.com.cn/s/blog_912673ce01013qq9.html" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;">http://blog.sina.com.cn/s/blog_912673ce01013qq9.html</a></p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>日志库<a href="https://github.com/Sirupsen/logrus" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;">logrus</a></h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">logrus是在Github中star最多的go日志库,功能强大,性能不错。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">安装:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">go&nbsp;get&nbsp;-u&nbsp;github.com/sirupsen/logrus</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">实例</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package&nbsp;mainimport&nbsp;(&nbsp;&nbsp;&nbsp;&quot;github.com/sirupsen/logrus&quot;&nbsp;&nbsp;&nbsp;&quot;os&quot;)//&nbsp;Create&nbsp;a&nbsp;new&nbsp;instance&nbsp;of&nbsp;the&nbsp;logger.&nbsp;You&nbsp;can&nbsp;have&nbsp;any&nbsp;number&nbsp;of&nbsp;instances.var&nbsp;log&nbsp;=&nbsp;logrus.New()func&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;//&nbsp;The&nbsp;API&nbsp;for&nbsp;setting&nbsp;attributes&nbsp;is&nbsp;a&nbsp;little&nbsp;different&nbsp;than&nbsp;the&nbsp;package&nbsp;level&nbsp;&nbsp;&nbsp;//&nbsp;exported&nbsp;logger.&nbsp;See&nbsp;Godoc.&nbsp;&nbsp;&nbsp;log.Out&nbsp;=&nbsp;os.Stdout&nbsp;&nbsp;&nbsp;//You&nbsp;could&nbsp;set&nbsp;this&nbsp;to&nbsp;any&nbsp;`io.Writer`&nbsp;such&nbsp;as&nbsp;a&nbsp;file&nbsp;&nbsp;&nbsp;file,&nbsp;err&nbsp;:=&nbsp;os.OpenFile(&quot;logrus.log&quot;,&nbsp;os.O_CREATE|os.O_WRONLY,&nbsp;0666)&nbsp;&nbsp;&nbsp;if&nbsp;err&nbsp;==&nbsp;nil&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.Out&nbsp;=&nbsp;file&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.Info(&quot;Failed&nbsp;to&nbsp;log&nbsp;to&nbsp;file,&nbsp;using&nbsp;default&nbsp;stderr&quot;)&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;log.WithFields(logrus.Fields{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;animal&quot;:&nbsp;&quot;walrus&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;size&quot;:&nbsp;&nbsp;&nbsp;10,&nbsp;&nbsp;&nbsp;}).Info(&quot;A&nbsp;group&nbsp;of&nbsp;walrus&nbsp;emerges&nbsp;from&nbsp;the&nbsp;ocean&quot;)}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">如果你对别的Go日志库感兴趣可以参考:</p><ul style="box-sizing: border-box; padding: 0px 0px 0px 2em; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;" class=" list-paddingleft-2"><li><p><a href="https://www.ctolib.com/topics-123640.html" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;">https://www.ctolib.com/topics-123640.html</a></p></li><li><p><a href="https://github.com/search?l=Go&o=desc&q=log&s=stars&type=Repositories" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;">https://github.com/search?l=Go&amp;o=desc&amp;q=log&amp;s=stars&amp;type=Repositories</a></p></li></ul><p><br/></p>

Linux入门学习之详解Linux命令提示符

<h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;">什么是命令提示符</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">细心的小伙伴可能已经发现,当我们远程登录到服务器,或者直接在Ubuntu桌面版打开Terminal时,当我们输入一条命令的时候,在光标的左侧有一串特殊格式的文本显示,这串文本就叫做命令提示符。<br/>如<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">root@hostname:</code></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><img src="https://www.maxiaoke.com/uploads/images/20231226/0a5fb0cbcc2fa464a519e32c624a710c.png" alt=""/></p><blockquote style="box-sizing: border-box; margin: 0px 0px 16px; border-left: 4px solid rgb(238, 238, 238); font-size: 14px; padding: 0px 15px 0px 20px; color: rgb(102, 102, 102); font-style: italic; font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px;">上图中标红的部分就是命令提示符。</p></blockquote><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">下面解释一下什么是命令提示符:<br/><img src="https://www.maxiaoke.com/uploads/images/20231226/de107458544a3713d4e0bdbb244a9863.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">可以看到,命令提示符主要提供以下几方面信息:</p><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;" class=" list-paddingleft-2"><li><p>你当前登录的用户身份</p></li><li><p>主机名</p></li><li><p>当前处于什么位置(目录)</p></li></ul><blockquote style="box-sizing: border-box; margin: 0px 0px 16px; border-left: 4px solid rgb(238, 238, 238); font-size: 14px; padding: 0px 15px 0px 20px; color: rgb(102, 102, 102); font-style: italic; font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px;">细心的同学会发现,当你用普通用户登录时,命令提示符最右侧显示的是<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">$</code>,而当我们用sudo命令切换为root用户时,该符号变成了<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">#</code></p></blockquote><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">切换为root用户时,显示的命令提示符:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><img src="https://www.maxiaoke.com/uploads/images/20231227/8d577b542edd626325efd88e21cb0f29.png" alt=""/></p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>whoami命令</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">除了通过命令提示符查看当前登录的用户外,还可以使用<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">whoami</code>命令可以显示当前登录的是什么用户:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><img src="https://www.maxiaoke.com/uploads/images/20231227/47b75c93d147ee5fe436377fbfd91c62.png" alt=""/></p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; color: rgb(77, 82, 89); text-wrap: wrap;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; transition: all 0.3s linear 0s; outline: none !important;"></a>一个特殊的目录 ~</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在linux的系统中,每个用户都有自己的一个家目录,这个家目录就可以理解为该用户的私有目录,用户可以在自己的家目录中创建目录,文件等操作。默认情况下,其它登录到系统的用户对于你的家目录下的文件是没有操作权限的,比如,不能在别人的家目录中增加文件,修改、删除文件等。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在Linux中,对于当前用户的家目录,用<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">~</code>符号表示。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">我们可以使用<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">cd ~</code>&nbsp;直接切换到当前用户的家目录。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><img src="https://www.maxiaoke.com/uploads/images/20231227/3f8696e24a0a278c55fa8f7bace11145.png" alt=""/></p><blockquote style="box-sizing: border-box; margin-top: 0px; margin-right: 0px; margin-left: 0px; border-left: 4px solid rgb(238, 238, 238); font-size: 14px; padding: 0px 15px 0px 20px; color: rgb(102, 102, 102); font-style: italic; font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap; margin-bottom: 0px !important;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px;">所有Linux系统用户的目录都统一存放在<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/home</code>下面,如,有一个用户名字叫zhangsan,那么他的家目录通常会在<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/home/zhangsan</code></p></blockquote><p><br/></p>

Linux入门学习之详细讲解Linux命令使用方法

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">这一小节我们先从前面的ls命令说起,在前面的内容中,我们曾经使用过<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">ls Videos</code>&nbsp;和 单独使用<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">ls</code>命令两种用法,那么他们的区别是什么呢?</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">ls Videos</code>:查看Videos目录下面有哪些内容<br/><code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">ls</code>:查看当前目录下有哪些内容</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">这里我们知道了,有的命令的用法后面可以加内容,也可以不加内容。这个是由命令本身的作用决定的。比如,之前我们用的<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">pwd</code>命令,这个命令的作用就是查看当前所在的目录位置,所以,他后面是不需要加参数的。</p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>Linux的命令格式</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><img src="https://www.maxiaoke.com/uploads/images/20231226/8b28bda7350b576acc79c3afd94cf9e2.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">通过前面的<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">ls</code>命令的示例,我们知道,在Linux命令行的世界里,一条命令究竟怎么使用,和你的需求是相关的。比如,你是想查看目录,还是切换目录,还是新建目录,新建文件..都有不同的命令。而具体使用哪个命令,要看你的需求是什么。</p><blockquote style="box-sizing: border-box; margin: 0px 0px 16px; border-left: 4px solid rgb(238, 238, 238); font-size: 14px; padding: 0px 15px 0px 20px; color: rgb(102, 102, 102); font-style: italic;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px;">每个命令的参数,即使长得一样,它的作用也可能不一样,所以,你得学会如何查看一个命令的参数。查看一个命令的参数的方法 : command —help。例如,我想要查看ls命令有哪些可用的参数,就可以通过输入 ls —help来查看。学会查看命令的帮助,对于你掌握linux命令有很大的帮助,要求我们必须会使用。</p></blockquote><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>Linux命令的一般格式</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><img src="https://www.maxiaoke.com/uploads/images/20231226/09dfd3cb0d745a2ce4c919ad8254037d.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">下面是一个<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">ls</code>命令的具体使用例子,展示了<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">ls</code>命令的使用:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><img src="https://www.maxiaoke.com/uploads/images/20231226/af803d0b6e52a6c5e662a40a14baa000.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">下面是一个使用 —help参数查看<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">rm</code>&nbsp;命令可以参数帮助文档的示例:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><img src="https://www.maxiaoke.com/uploads/images/20231226/c454ac1607605ebaf89477d9fbde195d.png" alt=""/></p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>授人以鱼,不如授人以渔</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">上面的内容中,我们以<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">ls</code>命令与<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">rm</code>命令为示例,为大家介绍了linux命令的基本用法,实际上,我们掌握到这种程度,就可以做到学会使用所有的Linux命令了。我们也不可能在这一篇文档中为大家展示所有Linux命令的用法和参数。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">剩下的部分就要靠大家自己去掌握,需要大家经常使用各种命令。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">如果掌握各种命令呢?</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">还是之前讲的道理,首先,我们要先要一个需求,比如,我想切换目录。那么,我们就可以去各种搜索 “在Linux中切换目录使用什么命令?” ,你肯定能得到一个答案:&nbsp;<code style="box-sizing: border-box; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">cd</code>&nbsp;命令。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">我们只需要知道某个需求对应的命令名字,就可以直接到我们的Linux系统中进行操作,如果想要知道这个命令可以加哪些参数,各个参数都表示什么意思,也只需要在我们的命令后面加上 —help 即可。</p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>命令在线学习</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">最后,给大家推荐一个学习Linux命令的网址:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><a href="https://www.linuxcool.com/" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">https://www.linuxcool.com/</a></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">这个页面提供了Linux常用命令的分类文档,大家可以查看学习。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px !important;"><img src="https://www.maxiaoke.com/uploads/images/20231226/be0116fe95f68132e7a058a6871e1aff.png" alt=""/></p><p><br/></p>