This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlets-go-01.html
1028 lines (949 loc) · 43.2 KB
/
lets-go-01.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title itemprop="name">Let's Go 1 — 变量与常量 - 真白的年轮面包</title>
<meta name="description" content="">
<meta name="keywords" content="golang">
<meta name="robots" content="none">
<meta name="generator" content="Halo 1.5.4">
<link rel="shortcut icon" type="images/x-icon" href="https://i.loli.net/2020/08/21/zYQoZvxB6eclsR5.png">
<link rel='stylesheet' href='static/css/style.min.css' type='text/css' media='all'>
<link rel="stylesheet" href="static/css/css.css" media="noexist" onload="this.media='all'">
<link rel="stylesheet" href="static/css/font_2010950_sh9o0r7ahar.css" media="noexist" onload="this.media='all'">
<link rel="stylesheet" href="static/css/lib.css" media="noexist" onload="this.media='all'">
<link rel='stylesheet' href="static/css/highlight-Dracula.css" type="text/css" media="noexist" onload="this.media='all'">
<link rel="stylesheet" href="static/css/jqcloud.min.css" media="noexist" onload="this.media='all'">
<link rel="stylesheet" href="static/css/justifiedGallery.min.css" media="noexist" onload="this.media='all'">
<style type="text/css"> .site-top .lower nav { display: block !important; } /** * 图片资源 */ .video-play, .loadvideo { background-image: url(static/image/[email protected]) } .video-pause { background-image: url(static/image/[email protected]) } #video-add { background-image: url(static/image/[email protected]); } #loading-comments { background-image: url(static/image/disqus-preloader.svg); } .headertop.filter-grid:before { background-image: url(static/image/grid.png) } .headertop.filter-dot:before { background-image: url(static/image/dot.gif) } @media (max-width:860px) { .headertop.filter-dot:before { background-image: url(static/image/grid.png) } } .search-form.is-visible { background-image: url(static/image/iloli.gif); } #pagination .loading, #journals-pagination .loading { background-image: url(static/image/rotating-ball-o.svg); } #banner_wave_1 { background-image: url(static/image/wave1.png) repeat-x; } #banner_wave_2 { background-image: url(static/image/wave2.png) repeat-x; } /** 鼠标样式 */ a { cursor: url(http://127.0.0.1:8090/themes/LIlGG_Sakura/source/cursor/ayuda.cur), auto } a:active { cursor: url(http://127.0.0.1:8090/themes/LIlGG_Sakura/source/cursor/work.cur),alias } p { cursor: url(http://127.0.0.1:8090/themes/LIlGG_Sakura/source/cursor/texto.cur),auto } body { cursor: url(http://127.0.0.1:8090/themes/LIlGG_Sakura/source/cursor/normal.cur),auto; } .cd-top { cursor: url(http://127.0.0.1:8090/themes/LIlGG_Sakura/source/cursor/No_Disponible.cur),auto; } .botui-actions-buttons-button { cursor: url(http://127.0.0.1:8090/themes/LIlGG_Sakura/source/cursor/No_Disponible.cur),auto; } .button.botui-actions-buttons-button { cursor: url(http://127.0.0.1:8090/themes/LIlGG_Sakura/source/cursor/No_Disponible.cur),auto; } .highlight-wrap code { cursor: url(http://127.0.0.1:8090/themes/LIlGG_Sakura/source/cursor/texto.cur),auto } .cd-top { background: url(https://cdn.jsdelivr.net/gh/LIlGG/[email protected]/img/Sakura/images/scroll.png) no-repeat center; } .author-profile i, .post-like a, .post-share .show-share, .sub-text, .we-info a, span.sitename, .post-more i:hover, #pagination a:hover, .post-content a:hover, .float-content i:hover { color: #fe9600 } .feature i, .feature-title span, .download, .navigator i:hover, .links ul li:before, .ar-time i, span.ar-circle, .object, .comment .comment-reply-link, .siren-checkbox-radio:checked + .siren-checkbox-radioInput:after { background: #fe9600 } ::-webkit-scrollbar-thumb { background: #fe9600 } .download, .navigator i:hover, .link-title, .links ul li:hover, #pagination a:hover, .comment-respond input[type='submit']:hover { border-color: #fe9600 } .entry-content a:hover, .site-info a:hover, .comment h4 a, #comments-navi a.prev, #comments-navi a.next, .comment h4 a:hover, .site-top ul li a:hover, .entry-title a:hover, #archives-temp h3, span.page-numbers.current, .sorry li a:hover, .site-title a:hover, i.iconfont.js-toggle-search.iconsearch:hover, .comment-respond input[type='submit']:hover { color: #fe9600 } #nprogress .bar { background: #fe9600 } #nprogress .peg { box-shadow: 0 0 10px #fe9600, 0 0 5px #fe9600; } #pagination a:hover { border: 1px solid #fe9600; } #pagination a:hover { -webkit-box-shadow: 0 0 4px #fe9600; -moz-box-shadow: 0 0 4px #fe9600; -o-box-shadow: 0 0 4px #fe9600; box-shadow: 0 0 4px #fe9600 } .entry-content a:after { background-color: #fe9600 } .site-top ul li a:after { background-color: #fe9600 } .post-tags a:hover { color: #fe9600 } blockquote:before { color: #fe9600; } blockquote:after { color: #fe9600; } @media (max-width: 860px) { #mo-nav ul li a:hover { color: #fe9600; } } @media (min-width: 860px) { .chip-container { margin-top: -60px;; } } #gallery-filter { text-align: center; } .masonry-gallery .gallery-item { margin-bottom: 10px } /** 日志 */ .journals-line>li:nth-child(odd) .journal-label:after { border-right-color: #e6e6fa; } .at_button { background-color: #e6e6fa; color: #ffffff; } .at_button:hover { background: #f0ffff; color: #ffffff; } .journals-line>li:nth-child(odd) .journal-label { background: linear-gradient(60deg,rgba(255, 165, 150, 0.5) 5%, rgba(0, 228, 255, 0.35) 95%) 0% 0% / cover, url(https://i.loli.net/2021/11/16/ycwC4MGToI5ngHl.jpg) 0% 0% / cover; color: #ffffff; } .journals-line>li .journal-label { background: linear-gradient(60deg,rgba(255, 165, 150, 0.5) 5%, rgba(0, 228, 255, 0.35) 95%) 0% 0% / cover, url(https://i.loli.net/2021/11/16/ycwC4MGToI5ngHl.jpg) 0% 0% / cover; color: #ffffff; } /*黑夜模式控件透明度*/ body.dark .header-info, body.dark .top-social img { color:#fff; background:rgba(0,0,0,0.7); } body.dark .notification, body.dark .the-feature.from_left_and_right .info { background-color: rgba(0,0,0,0.7); } body.dark .skin-menu, body.dark .m-cd-top { background-color:rgba(0,0,0,0.7) !important; } /*黑夜模式图像亮度*/ body.dark img, body.dark .highlight-wrap, body.dark iframe, body.dark .entry-content .aplayer { filter:brightness(0.7); } /*黑夜模式主题色*/ body.dark .ar-time i, body.dark span.ar-circle, body.dark .scrollbar, body.dark .butterBar-message, body.dark .aplayer .aplayer-list ol li:hover { background: #fe9600 !important; } body.dark .aplayer .aplayer-list ol li.aplayer-list-light .aplayer-list-cur, body.dark .user-menu-option a:hover , body.dark .menu-list li:hover , body.dark .font-family-controls button:hover , body.dark .openNav .icon, body.dark .openNav .icon:before , body.dark .openNav .icon:after , body.dark .openNav .icon:after , body.dark .site-top ul li a:after { background-color: #fe9600; } body.dark #archives-temp h3, body.dark #moblieGoTop, body.dark #changskin, body.dark .the-feature.from_left_and_right a:hover .info p, body.dark .the-feature.from_left_and_right .info, body.dark .ins-section .ins-search-item:hover, body.dark .ins-section .ins-search-item:hover .ins-slug, body.dark .ins-section .ins-search-item:hover .ins-search-preview, body.dark .ins-section .ins-search-item:hover .iconfont , body.dark .float-content i:hover, body.dark .menhera-container .emoji-item:hover , body.dark .comment-respond .logged-in-as a:hover , body.dark .site-top ul li a:hover , body.dark i.iconfont.js-toggle-search.iconsearch:hover { color: #fe9600; } body.dark .aplayer .aplayer-info .aplayer-controller .aplayer-time .aplayer-icon:hover path { fill: #fe9600 } body.dark #moblieGoTop:hover , body.dark #changskin:hover { color: #fe9600; opacity:.8; } body.dark .focusinfo .header-tou img { box-shadow: inset 0 0 10px #fe9600; } </style> <!-- 评论模块独立样式 --> <style id="comment-style" type="text/css" media="noexist"> .halo-comment.dark button, .halo-comment.dark input, .halo-comment.dark select, .halo-comment.dark textarea, .halo-comment.dark i.iconfont.js-toggle-search.iconsearch, .halo-comment.dark .comment-textarea .input-label, .halo-comment.dark #emotion-toggle span, .halo-comment.dark .emotion-box, .halo-comment.dark .emotion-box .motion-container .emoji-item { color: #eee !important; } .halo-comment.dark .body p { color: #bebebe !important; } .halo-comment.dark input[type=color]:focus, .halo-comment.dark input[type=date]:focus, .halo-comment.dark input[type=datetime-local]:focus, .halo-comment.dark input[type=datetime]:focus, .halo-comment.dark input[type=email]:focus, .halo-comment.dark input[type=month]:focus, .halo-comment.dark input[type=number]:focus, .halo-comment.dark input[type=password]:focus, .halo-comment.dark input[type=range]:focus, .halo-comment.dark input[type=search]:focus, .halo-comment.dark input[type=tel]:focus, .halo-comment.dark input[type=text]:focus, .halo-comment.dark input[type=time]:focus, .halo-comment.dark input[type=url]:focus, .halo-comment.dark input[type=week]:focus, .halo-comment.dark textarea:focus { color: #eee !important; background-color: #31363b !important; } .halo-comment.dark .comment .info, .halo-comment.dark .comment .comment-time, .halo-comment.dark .comment-respond .logged-in-as, .halo-comment.dark .notification, .halo-comment.dark .comment-respond .logged-in-as a { color: #9499a8; } /** 说说评论样式 */ .halo-comment.journal { max-height: 600px; overflow: auto; } </style> <script type="text/javascript">
if (!!window.ActiveXObject || "ActiveXObject" in window) {
alert('请抛弃万恶的IE系列浏览器吧。');
}
</script>
</head>
<body class="hfeed chinese-font serif">
<section id="main-container">
<div class="headertop filter-dim">
<figure id="centerbg" class="centerbg" style="background-image: url(
static/image/XG5bOgFJek6v2NH.jpg
);
">
<div class="focusinfo">
<h1 class="center-text glitch" data-text="Shiina Mashiro">Shiina Mashiro</h1>
<div class="header-info no-select
info-desc"><p><i class="fa fa-quote-left"></i><span class="desc"> 此 生 无 悔 恋 真 白 ,来 世 愿 入 樱 花 庄 。</span><i class="fa fa-quote-right"></i></p>
<div class="top-social_v2">
<li id="bg-pre">
<img class="flipx" src="static/picture/next-b.svg">
</li>
<li>
<a href="javascript:;" target="_blank" class="social-github i18n" data-iname="icon_alt.github" data-iattr="title">
<img src="static/picture/github.png">
</a>
</li>
<li>
<a href="javascript:;" target="_blank" class="social-bili i18n" data-iname="icon_alt.bili" data-iattr="title">
<img src="static/picture/bilibili.png">
</a>
</li>
<li>
<a href="javascript:;" target="_blank" class="i18n" data-iname="icon_alt.twitter" data-iattr="title">
<img src="static/picture/twitter.png">
</a>
</li>
<li>
<a href="javascript:;" target="_blank" class="i18n" data-iname="icon_alt.stackoverflow" data-iattr="title">
<img src="static/picture/stackoverflow.svg">
</a>
</li>
<li id="bg-next">
<img src="static/picture/next-b.svg">
</li>
</div>
</div>
</div>
</figure>
<!-- 背景视频 -->
<div id="video-container">
<video id="bgvideo" class="video" data-id="" data-server="bilibili" data-url="/sakurasou.flv" width="auto" preload="auto" data-cid="" data-qn="64" data-vtype="0"></video>
<div id="video-btn" class="loadvideo videolive"></div>
<div id="video-add"></div>
<div class="video-stu"></div>
</div> <!-- 首页下拉箭头 -->
<div class="headertop-down faa-float animated" onclick="headertop_down()">
<span><i class="fa fa-chevron-down" aria-hidden="true"></i></span>
</div>
</div>
<div id="page" class="site wrapper">
<style>
/** Post start */
/** 文章页面导航栏自动收缩 */
.yya:not(.sabit) {
top: -100px;
transition: all .4s ease;
}
.sabit {
transition: all .4s ease;
}
/** 文章页首行缩进 */
/** Post end */
</style> <header class="site-header " role="banner">
<div class="site-top">
<div class="site-branding">
<div class="site-title">
<a href="index.html">
<img src="static/picture/VByUkvYofXhMP2H.png">
</a>
</div>
</div><!-- .site-branding -->
<div class="header-user-avatar">
<img src="static/picture/avatar.jpg" width="30" height="30">
<div class="header-user-menu">
<div class="herder-user-name">
<div class="herder-user-name-u">Parasomnia</div>
</div>
</div>
</div>
<div class="searchbox"><i class="iconfont js-toggle-search iconsearch icon-search"></i></div>
<div class="lower-cantiner">
<div class="lower">
<nav class="navbar">
<ul id="menu-menu-1" class="menu">
<li>
<a href="index.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-fort-awesome faa-tada" aria-hidden="true"></i>
首页
</span>
</a>
</li>
<li>
<a href="archives.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-archive faa-tada" aria-hidden="true"></i>
归档
</span>
</a>
</li>
<li>
<a href="categories.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-folder-open faa-tada" aria-hidden="true"></i>
分类
</span>
</a>
</li>
<li>
<a href="tags.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-tags faa-tada" aria-hidden="true"></i>
标签
</span>
</a>
</li>
<li>
<a href="journals.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-calendar-o faa-tada" aria-hidden="true"></i>
足迹
</span>
</a>
</li>
<li>
<a href="board.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-pencil faa-tada" aria-hidden="true"></i>
留言
</span>
</a>
</li>
<li>
<a href="links.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-link faa-shake" aria-hidden="true"></i>
友链
</span>
</a>
</li>
<li>
<a href="about.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-leaf faa-tada" aria-hidden="true"></i>
关于
</span>
</a>
</li>
<li>
<a href="javascript:;" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-rss faa-tada" aria-hidden="true"></i>
订阅
</span>
</a>
</li>
</ul> </nav>
<!-- #site-navigation -->
</div>
</div>
</div>
</header><!-- #masthead -->
<div class="pattern-center-blank"></div>
<div class="pattern-center single-center no-select">
<div class="pattern-attachment-img">
<img class="lazyload" data-src="static/image/gPwDKkpz2qb8fxu.png" src="static/picture/orange.progress-bar-stripe-loader.svg" onerror="imgError(this)">
</div>
<header class="pattern-header single-header">
<h1 class="entry-title">Let's Go 1 — 变量与常量</h1>
<p class="entry-census">
<span>
<a href="index.html">
<img src="static/picture/avatar.jpg">
</a>
</span>
<span>
<a href="index.html">Parasomnia</a>
</span>
<span class="bull">·</span><span class="i18n" data-iname="post.time" data-ivalue="2020-03-08"></span>
<span class="bull">·</span><span class="i18n" data-iname="post.edit_time" data-ivalue="2020-09-16"></span>
<span class="bull">·</span><span class="i18n" data-iname="post.visits" data-ivalue="767"></span>
</p>
</header>
</div>
<div id="content" class="site-content">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<article id="post-33">
<div class="toc-container">
<div class="toc"></div>
</div>
<div class="entry-content">
<h1 id="变量">变量</h1>
<h2 id="命名规范">命名规范</h2>
<p>通常采用驼峰式命名,当遇到特有名词(缩写或简称,如DNS)的时候,特有名词根据是否私有全部大写或小写。</p>
<p>形如:<code>apiClient</code> 、<code>URLString</code> 注意这里的 <code>api</code> (全小写) 和 <code>URL</code>(全大写)</p>
<h2 id="全局变量的声明与赋值">全局变量的声明与赋值</h2>
<pre><code class="language-go">package main
import "fmt"
// 指定类型的变量声明:如果没有为变量赋值,就需要指定变量的类型
var a,b,c int
// 变量赋值会自动匹配类型,无需指定
var d = 1 // 等同于 var d int = 1
// 一次赋值多个变量
var e,f,g = 1,2,3
// 变量代码块
var (
h = 4
i = 5
j,k = 6,7
)
func main(){
...
}
</code></pre>
<h2 id="局部变量的声明与赋值">局部变量的声明与赋值</h2>
<pre><code class="language-go">package main
import "fmt"
func main(){
// 除了上面讲到的声明方式外,函数体内可以通过更简单的方式定义临时变量
a := 6
// 也可以一次多定义几个
b,c := 8,10
// 声明的局部变量如果没被使用就会报错
fmt.Println(a,b,c)// 输出结果 6,8,10
}
</code></pre>
<p>**注意:**如果变量类型不足以存储变量值会导致溢出错误</p>
<pre><code class="language-go">var num byte = 1000000 // overflows
</code></pre>
<h3 id="多变量赋值">多变量赋值</h3>
<p><strong>多变量赋值时,先计算所有相关值,然后再从左到右依次赋值</strong></p>
<p>看下面代码</p>
<pre><code class="language-go">package main
import "fmt"
func main() {
data, i := [3]int{1,2,3},0
i,data[i] = 2,5
fmt.Println(data,i,data[i])// 输出结果:[5 2 3] 2 3
}
</code></pre>
<blockquote>
<p>问:为什么不是输出 [1,2,5] 2 3 ?</p>
<p>答:先计算,后赋值,data[i]中的i被当作0看待<br>
<strong>即 <code>i,data[i] = 2, 5</code> 当作 <code>i,data[0] = 2,5</code></strong></p>
</blockquote>
<p>因此,如果你想交换两个元素的值就变得非常简单了</p>
<pre><code class="language-go">//交换i和j的值
i,j := j,i
</code></pre>
<h3 id="多变量赋值的占位符">多变量赋值的占位符</h3>
<p>如果函数返回多个返回值,而你只想要把其中的一个或几个赋值给相应的变量,可以使用占位符 <code>_</code></p>
<pre><code class="language-go">package main
import "fmt"
// 指定方法的返回类型
func test() (int, string) {
return 1, "abc"
}
func main() {
_, s := test()
fmt.Println(s)// s获取到test()的第二个返回值,输出abc
}
</code></pre>
<h2 id="同名变量的优先级">同名变量的优先级</h2>
<p><strong>同java相同,作用域越小的变量优先级就越高</strong></p>
<pre><code class="language-go">package main
import "fmt"
var num = 1
func main() {
fmt.Println(num)// 使用全局变量,输出1
num := 2
fmt.Println(num)// 使用main中的局部变量,输出2
{
num := 3
fmt.Println(num)// 使用代码块中的局部变量,输出3
}
}
</code></pre>
<h1 id="常量">常量</h1>
<p><strong>常量值</strong> <strong>必须</strong>是<strong>编译器可确定</strong>的 <strong>数字</strong>、<strong>字符串</strong>、<strong>布尔值。</strong><br>
这意味着,<strong>常量必须在声明的时候初始化</strong></p>
<h2 id="命名规范-1">命名规范</h2>
<p>同变量规则,力求语义表达完整清楚,不要嫌名字长。<br>
如果模块复杂,为避免混淆,可按功能统一定义在package下的一个文件中。<br>
通常也使用驼峰命名法</p>
<h2 id="常量与常量组的声明">常量与常量组的声明</h2>
<pre><code class="language-go">const x, y int = 1, 2 // 多常量初始化
const s = "Hello, World!" // 类型推断
const ( // 常量组
a, b = 10, 100
c bool = false
d //如果不提供类型和初始化值则视为和上一常量相同,即 d = false
//常量值可以使用len、cap、unsafe.Sizeof等编译器可确定的函数返回值
e = len(a)
f = unsafe.Sizeof(b)
)
func main() {
const x = "xxx" // 未使⽤用局部常量不会引发编译错误。
}
</code></pre>
<p>**注意:**如果常量类型不足以存储变量值会导致溢出错误</p>
<pre><code class="language-go">const num byte = 1000000 // overflows
</code></pre>
<h2 id="枚举">枚举</h2>
<p>枚举其实就是一个带自增特性的常量组,而这个自增特性是通过关键字 <code>iota</code> 实现的。</p>
<blockquote>
<p>iota是golang语言的常量计数器,只能在常量的表达式中使用。<br>
iota 定义常量组中从 0 开始按行计数的自增枚举值。</p>
</blockquote>
<pre><code class="language-go">const (
Sunday = iota // 0
Monday // 1,通常省略后续⾏行表达式。
Tuesday // 2
Wednesday // 3
Thursday // 4
Friday // 5
Saturday // 6
)
</code></pre>
<p>由于常量赋值是可以用编译器能解释的计算式表达的,所以就出现了下面的骚操作</p>
<pre><code class="language-go">const (
_ = iota // iota = 0
KB int64 = 1 << (10 * iota) // iota = 1
MB // 与 KB 表达式相同,但 iota = 2
GB // 同上,但 iota = 3
TB // 同上,但 iota = 4
)
</code></pre>
<p>上述方式在用于表达具有<strong>数量级关系</strong>的枚举时特别有用</p>
<h3 id="多个iota的各自增长">多个iota的各自增长</h3>
<p>在同一常量组中,可以提供多个 iota,它们<strong>各自增长,互不影响</strong>。</p>
<pre><code class="language-go">const (
A, B = iota, iota << 10 // 0, 0 << 10
C, D // 1, 1 << 10
)
</code></pre>
<p>**技巧:**一列一列的看,同一列上的枚举项共用计数器 <code>iota</code></p>
<h3 id="自增中出现显示赋值">自增中出现显示赋值</h3>
<pre><code class="language-go">const (
A = iota // 0
B // 1
C = "c" // c
D // c,与上⼀一⾏行相同。
E = iota // 4,显式恢复。注意计数包含了 C、D 两⾏行。
F // 5
)
</code></pre>
<h3 id="通过自定义类型实现枚举类型限制">通过自定义类型实现枚举类型限制</h3>
<pre><code class="language-go">package main
import "fmt"
// 声明一种叫Color的类型,其本质是一个int
type Color int
const (
Black Color = iota // 定义一个Color类型的枚举项
Red // ...
Blue // ...
)
//必须传入枚举类型Color的函数
func test(c Color) {
fmt.Println(c)
}
func main() {
c := Black
test(c) //输出0
x := 1
test(x) // Error: cannot use x (type int) as type Color in function argument
test(1) // 常量会被编译器自动转换,不会报错
test(10) // 常量只要类型与枚举类型一致即可,即便其值不被枚举所包括也不会报错
const numd = 10
test(numd) // 常量只要类型与枚举类型一致即可,即便其值不被枚举所包括也不会报错
}
</code></pre>
<p>也就是说<strong>类型限制只能限制变量</strong></p>
<h1 id="参考与补充">参考与补充</h1>
<ul>
<li><a href="javascript:;">Go 边看边练</a></li>
<li><a href="javascript:;">Golang命名规范和开发规范</a></li>
</ul>
<p>Q.E.D. <i class="fa fa-meetup" aria-hidden="true" style="color:#d34836"></i></p>
</div><!-- .entry-content -->
<div class="single-reward">
<div class="reward-open"><span class="i18n" data-iname="post.reward"></span>
<div class="reward-main">
<ul class="reward-row">
<li class="alipay-code"><img src="https://i.loli.net/2020/08/21/iotaKjFTfAsh6qE.jpg"></li>
<li class="wechat-code"><img src="static/picture/HfcFv3kP18y4TGW.jpg"></li>
</ul>
</div>
</div>
</div>
<footer class="post-footer">
<div class="post-license">
<a href="javascript:;" target="_blank" rel="nofollow">
<i class="fa fa-creative-commons" aria-hidden="true"></i>
<span class="i18n" data-iname="post.creative_commons"></span>
</a>
</div>
<div class="post-tags">
<i class="iconfont icon-tags"></i>
<a href="golang.html" rel="tag">golang</a>
</div>
<style>
.s-wechat {
position: relative;
}
.s-wechat .wechat-qrcode {
display: none;
border: 1px solid #eee;
position: absolute;
top: -215px;
left: -84px;
width: 200px;
height: 192px;
color: #666;
font-size: 12px;
text-align: center;
background-color: #fff;
box-shadow: 0 2px 10px #aaa;
transition: all 200ms;
-webkit-tansition: all 350ms;
-moz-transition: all 350ms
}
.s-wechat .wechat-qrcode.bottom {
top: 40px;
left: -84px
}
.s-wechat .wechat-qrcode.bottom:after {
display: none
}
.s-wechat .wechat-qrcode h4 {
font-weight: normal;
height: 26px;
line-height: 26px;
font-size: 12px;
background-color: #f3f3f3;
margin: 0;
padding: 0;
color: #777
}
.s-wechat .wechat-qrcode .qrcode {
width: 105px;
margin: 10px auto
}
.s-wechat .qrcode table {
margin: 0 !important
}
.s-wechat .wechat-qrcode .help p {
font-weight: normal;
line-height: 16px;
padding: 0;
margin: 0
}
.s-wechat .wechat-qrcode:after {
content: '';
position: absolute;
left: 50%;
margin-left: -6px;
bottom: -13px;
width: 0;
height: 0;
border-width: 8px 6px 6px 6px;
border-style: solid;
border-color: #fff transparent transparent transparent
}
.s-wechat:hover .wechat-qrcode {
display: block
}
.s-wechat .wechat-qrcode img {
width: auto;
height: auto;
}
</style>
<div class="post-share">
<ul class="social-share sharehidden">
<li>
<a href="javascript:;" onclick="window.open(this.href, 'weibo-share', 'width=490,height=700');return false;" class="s-weibo i18n" data-iname="share.weibo" data-iattr="title">
<img src="static/picture/weibo.png">
</a>
</li>
<li>
<a href="javascript:;" onclick="window.open(this.href, 'qq-share', 'width=730,height=500');return false;" class="s-qq i18n" data-iname="share.qq" data-iattr="title">
<img src="static/picture/qq.png">
</a>
</li>
<li>
<a href="javascript:" class="s-wechat">
<div class="wechat-qrcode">
<h4 class="i18n" data-iname="share.wechat.qcode_title"></h4>
<div id="qrcode" class="qrcode" title="http://127.0.0.1:8090/archives/lets-go-01" data-url="http://127.0.0.1:8090/archives/lets-go-01"></div>
<div class="help">
<p class="i18n" data-iname="share.wechat.help_1"></p>
<p class="i18n" data-iname="share.wechat.help_2"></p>
</div>
</div>
<img src="static/picture/wechat.png">
</a>
</li>
<li>
<a href="javascript:;" onclick="window.open(this.href, 'douban-share', 'width=490,height=600');return false;" class="s-douban i18n" data-iname="share.douban" data-iattr="title">
<img src="static/picture/douban.png">
</a>
</li>
<li>
<a href="javascript:;" onclick="window.open(this.href, 'qzone-share', 'width=490,height=600');return false;" class="s-qzone i18n" data-iname="share.qzone" data-iattr="title">
<img src="static/picture/qzone.png">
</a>
</li>
<li>
<a href="javascript:;" onclick="window.open(this.href, 'linkedin-share', 'width=490,height=600');return false;" class="s-linkedin i18n" data-iname="share.linkedin" data-iattr="title">
<img src="static/picture/linkedin.png">
</a>
</li>
</ul>
<i class="iconfont show-share icon-forward"></i>
</div>
</footer><!-- .entry-footer -->
</article><!-- #post-## -->
<section class="post-squares nextprev">
<div class="post-nepre half previous">
<a href="lets-go-02.html" rel="prev">
<div class="background" style="background-image:url('static/image/gPwDKkpz2qb8fxu.png');"></div>
<span class="label i18n" data-iname="post.prev"></span><div class="info"><h3>Let's Go 2 — 类型与字符串</h3><hr></div>
</a>
</div>
<div class="post-nepre half next">
<a href="lets-go-00.html" rel="next">
<div class="background" style="background-image:url('static/image/gPwDKkpz2qb8fxu.png');"></div>
<span class="label i18n" data-iname="post.next"></span><div class="info"><h3>Let's Go 0 — 项目创建、命名规范和包导入导出</h3><hr></div>
</a>
</div>
</section>
<section class="author-profile">
<div class="info" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
<a href="index.html" class="profile gravatar"><img src="static/picture/avatar.jpg" itemprop="image" alt="Parasomnia" height="70" width="70"></a>
<div class="meta">
<span class="title">Parasomnia</span>
<h3 itemprop="name">
<a href="index.html" itemprop="url" rel="author">Parasomnia</a>
</h3>
</div>
</div>
<hr>
<p><i class="iconfont icon-pencil"></i>
<span> 此 生 无 悔 恋 真 白 ,来 世 愿 入 樱 花 庄 。</span>
</p>
</section>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- #content -->
<!-- 定义可变属性,会根据页面的改变而变化 -->
<script type='text/javascript'>
/* <![CDATA[ */
var PageAttr = {
"metas": {
},
"isPost": "true",
"postWordCount": "3,703",
"postEditTime": "2020-09-16 00:18:26"
}
/* ]]> */
</script>
</div><!-- #page Pjax container-->
<footer id="colophon" class="site-footer" role="contentinfo">
<div class="site-info">
<div class="footertext">
<p class="foo-logo" style="background-image: url('static/image/sakura.svg');"></p>
ねえ、あなたは何になりたいの色?
</div>
<div class="footer-device">
<p style="font-family: 'Ubuntu', sans-serif;">
<span>Powered
<i class="fa fa-vimeo animated" style="color: #e74c3c;"></i>
by
<a rel="me" target="_blank" href="javascript:;" title="一款优秀的开源博客内容发布系统" style="text-decoration:none;">Halo</a>
</span>
•
<span>Crafted with
<i class="fa fa-heart animated" style="color: #e74c3c;"></i>
by
<a rel="me" target="_blank" href="javascript:;" style="text-decoration:none;">LIlGG</a>
</span>
</p>
<p>
© 2022 Parasomnia
</p>
</div>
</div><!-- .site-info -->
</footer><!-- #colophon -->
<div class="openNav">
<div class="iconflat">
<div class="icon"></div>
</div>
<div class="site-branding">
<div class="site-title">
<a href="index.html">
<img src="static/picture/VByUkvYofXhMP2H.png">
</a>
</div>
</div>
</div><!-- m-nav-bar -->
</section><!-- #section -->
<!-- m-nav-center -->
<div id="mo-nav">
<div class="m-avatar">
<img src="static/picture/avatar.jpg">
</div>
<p style="text-align: center; color: #333; font-weight: 900; font-family: 'Ubuntu', sans-serif; letter-spacing: 1.5px">Shiina Mashiro</p>
<p style="display:flex; justify-content:center;">
<a href="javascript:;" class="social social-twitter" target="_blank"><img src="static/picture/twitter.png" width="18"></a>
<a href="javascript:;" class="social social-github" target="_blank"><img src="static/picture/github.png" width="18"></a>
<a href="javascript:;" class="social social-bili" target="_blank"><img src="static/picture/bilibili.png" width="18"></a>
</p>
<div class="m-search">
<form class="m-search-form" method="get" action="/search" role="search">
<input class="m-search-input" type="search" name="keyword" placeholder="搜索..." required="">
</form>
</div>
<ul id="menu-menu-1" class="menu">
<li>
<a href="index.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-fort-awesome faa-tada" aria-hidden="true"></i>
首页
</span>
</a>
</li>
<li>
<a href="archives.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-archive faa-tada" aria-hidden="true"></i>
归档
</span>
</a>
</li>
<li>
<a href="categories.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-folder-open faa-tada" aria-hidden="true"></i>
分类
</span>
</a>
</li>
<li>
<a href="tags.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-tags faa-tada" aria-hidden="true"></i>
标签
</span>
</a>
</li>
<li>
<a href="journals.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-calendar-o faa-tada" aria-hidden="true"></i>
足迹
</span>
</a>
</li>
<li>
<a href="board.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-pencil faa-tada" aria-hidden="true"></i>
留言
</span>
</a>
</li>
<li>
<a href="links.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-link faa-shake" aria-hidden="true"></i>
友链
</span>
</a>
</li>
<li>
<a href="about.html" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-leaf faa-tada" aria-hidden="true"></i>
关于
</span>
</a>
</li>
<li>
<a href="javascript:;" target="_self">
<span class="faa-parent animated-hover">
<i class="fa fa-rss faa-tada" aria-hidden="true"></i>
订阅
</span>
</a>
</li>
</ul> <p class="m-footer">© 2022 Parasomnia</p>
</div><!-- m-nav-center end -->
<a href="#" class="cd-top"></a>
<!-- m-cd-top start -->
<button class="m-cd-top" title="Go to top">
<i class="fa fa-chevron-up" aria-hidden="true"></i>
</button>
<!-- m-cd-top end -->
<!-- search start -->
<form class="js-search search-form search-form--modal" method="get" action="/search" role="search">
<div class="search-form__inner">
<div>
<p class="micro mb-">输入后按回车搜索 ...</p>
<i class="iconfont icon-search"></i>
<input class="text-input" type="search" name="keyword" placeholder="Search" required="">
</div>
</div>
<div class="search_close"></div>
</form>
<!-- search end -->
<!-- aplayer start -->
<!-- aplayer end -->
<!-- theme-change start -->
<div class="changeSkin-gear no-select">
<div class="keys">
<span id="open-skinMenu">切换主题 | SCHEME TOOL <i class="iconfont icon-gear inline-block rotating"></i></span>
</div>
</div>
<div class="skin-menu no-select">
<div class="theme-controls row-container">
<ul class="menu-list">
<li id="bg_0" data-text="默认是纯洁的白色~">
<i class="fa fa-television" aria-hidden="true"></i>
</li>
<li id="bg_1" data-text="樱花飞舞~">
<i class="iconfont icon-sakura" aria-hidden="true"></i>
</li>
<li id="bg_2" data-text="格子控!">
<i class="fa fa-slack" aria-hidden="true"></i>
</li>
<li id="bg_3" data-text="小点点~">
<i class="iconfont icon-dots" aria-hidden="true"></i>
</li>
<li id="bg_4" data-text="充满力量的橙色">
<i class="fa fa-optin-monster" aria-hidden="true"></i>
</li>
<li id="bg_5" data-text="pixiv主题~">
<i class="iconfont icon-pixiv" aria-hidden="true"></i>
</li>
<li id="bg_6" data-text="真白像素风格">
<i class="fa fa-smile-o" aria-hidden="true"></i>
</li>
<li id="bg_7" data-text="关灯!">
<i class="fa fa-moon-o" aria-hidden="true"></i>
</li>
</ul>
</div>
</div>
<!-- theme-change end -->
<!-- 定义全局属性 -->
<script type='text/javascript'>
/* <![CDATA[ */
var Poi = {
"pjax":"true",
"windowheight":"auto",
"ajaxurl":"http://127.0.0.1:8090",
"resBaseUrl": "http://127.0.0.1:8090/themes/LIlGG_Sakura",
"formpostion":"bottom",
"toc": "true",
"codeLine": "true",
"themeChange": "true",
"headFocus": "true",
"bgvideo": "true",
"tagRandomColorMin": "0.999",
"tagRandomColorMax": "1",
"nickname": "Parasomnia",
"sitename": "真白的年轮面包",
"themeBase": "http://127.0.0.1:8090/themes/LIlGG_Sakura",
"openToast": "true",
"toastWidth": 260,
"toastHeight": 60,
"toastTop": "top",
"toastBackground": "#fe9600",
"toastColor": "#ffffff",
"toastFontSize": 14,
"copyMonitor": "true",
"copyrightNotice": "true",
"photosStyle": "justify",
"photosGutter": 10,
"tocDepth": 0,
"i18n": "auto",
"coverNum": "5",
"rimageUrl": "https://api.lixingyong.com/api/images",
"coverOpen": "",
"meEmail": "",
"defaultTheme": "bg_0",
"defaultGroup": "",
"isPostWordCountToast": "",
"isPostEditTimeToast": "",
"postWordCountToastNormal": "文章篇幅适中,可以放心阅读。",
"postWordCountToastMedium": "文章内容较长,请提前准备好咖啡!!!",
"postWordCountToastDifficulty": "文章内容很长,建议分段阅读。",
"postEditTimeToastNormal": "近期有所更新,请放心阅读!",
"postEditTimeToastMedium": "文章距上次编辑时间较远,部分内容可能已经过时!",
"postEditTimeToastDifficulty": "文章内容已经很陈旧了,也许不再适用!",
"journalLikes": "",
"journalComment": "",
};
var bgConfig = {
"bg_0": {
"id": "0",
"name": "white",
"desc": "默认是纯洁的白色~",
"url": "",
"strategy": "none",
"isNight": ""
},
"bg_1": {
"id": "1",
"name": "sakura",
"desc": "樱花飞舞~",
"url": "https://view.lixingyong.com/images/2020/07/23/695cc3a80b21ee7f18bd731824ab9638.png",
"strategy": "none",
"isNight": ""
},
"bg_2": {
"id": "2",
"name": "gribs",
"desc": "格子控!",
"url": "https://view.lixingyong.com/images/2020/07/23/defc1b6783cd16774900517d3b38ded7.jpg",
"strategy": "none",
"isNight": ""
},
"bg_3": {
"id": "3",
"name": "KAdots",
"desc": "小点点~",
"url": "https://view.lixingyong.com/images/2020/07/23/kyotoanimation.png",
"strategy": "none",
"isNight": ""
},
"bg_4": {
"id": "4",
"name": "totem",
"desc": "充满力量的橙色",
"url": "https://view.lixingyong.com/images/2020/07/23/little-monster.png",
"strategy": "none",
"isNight": ""
},
"bg_5": {
"id": "5",
"name": "pixiv",
"desc": "pixiv主题~",
"url": "https://view.lixingyong.com/images/2020/07/23/star02.png",
"strategy": "none",
"isNight": ""
},
"bg_6": {
"id": "6",
"name": "Mashiro pixel",
"desc": "真白像素风格",
"url": "https://i.loli.net/2020/09/30/YbVaGTp8UkR4gHP.png",
"strategy": "cover",
"isNight": ""
},
"bg_7": {
"id": "7",
"name": "dark",
"desc": "关灯!",
"url": "",
"strategy": "none",
"isNight": "true"
},
};
/* ]]> */
</script>
<script type='text/javascript' src='static/js/utils.min.js'></script>
<script type="text/javascript" src="static/js/lazysizes.min.js" async=""></script>
<script type="text/javascript" src="static/js/lib.js"></script>
<!-- 相册 -->
<script src="static/js/jquery.justifiedGallery.min.js" async=""></script>
<script type="text/javascript" src="static/js/highlight.pack.js" defer=""></script>
<script type="text/javascript" src="static/js/highlightjs-line-numbers.min.js" defer=""></script>
<script src="static/js/tocbot.min.js" defer=""></script>
<script src="static/js/sakura-comment.min.js" defer=""></script>
<script src="static/js/jqcloud.min.js" defer=""></script>
<script type='text/javascript'>
var wordcloud = [
{'text': '网络', 'weight': '1', 'link': 'http://127.0.0.1:8090/tags/网络'},
{'text': '文件系统', 'weight': '1', 'link': 'http://127.0.0.1:8090/tags/文件系统'},
{'text': '硬件', 'weight': '1', 'link': 'http://127.0.0.1:8090/tags/硬件'},
{'text': 'oauth2', 'weight': '1', 'link': 'http://127.0.0.1:8090/tags/oauth2'},
{'text': 'ssh', 'weight': '1', 'link': 'http://127.0.0.1:8090/tags/ssh'},
{'text': 'python', 'weight': '3', 'link': 'http://127.0.0.1:8090/tags/python'},
{'text': '中间件', 'weight': '1', 'link': 'http://127.0.0.1:8090/tags/中间件'},
{'text': 'http', 'weight': '1', 'link': 'http://127.0.0.1:8090/tags/http'},
{'text': '算法', 'weight': '3', 'link': 'http://127.0.0.1:8090/tags/算法'},
{'text': 'golang', 'weight': '17', 'link': 'http://127.0.0.1:8090/tags/golang'},
{'text': 'android', 'weight': '2', 'link': 'http://127.0.0.1:8090/tags/android'},
{'text': '认知', 'weight': '3', 'link': 'http://127.0.0.1:8090/tags/认知'},
{'text': 'git', 'weight': '3', 'link': 'http://127.0.0.1:8090/tags/git'},
{'text': 'hard times', 'weight': '6', 'link': 'http://127.0.0.1:8090/tags/hardtime'},
{'text': '心理', 'weight': '5', 'link': 'http://127.0.0.1:8090/tags/心理'},
{'text': 'java', 'weight': '2', 'link': 'http://127.0.0.1:8090/tags/java'},