首页
视频
资源
登录
原
Go语言基础(二)变量,常量以及与其他语言的差异
4523
人阅读
2020/5/23 15:17
总访问:
2617543
评论:
0
收藏:
0
手机
分类:
Go
![go语言基础一](https://img.tnblog.net/arcimg/hb/0dd243ae49394f12beed84237bfee1a3.jpg "go语言基础一") >#Go语言基础(二)变量,常量以及与其他语言的差异 [TOC] <br/> 编写测试程序 ------------ <br/> >###注意事项 1. 源码文件以_test结尾:xxx_test.go 2. 测试方法名以Test开头:func Testxxx(t *testing.T){...} >###编写简单的测试代码 <br/> >first_test.go ```go package try_test import ( "testing" ) func TestFirstTry(t *testing.T) { t.Log("My first try") //测试日志输出 t.Error("Over") //测试问题输出 } ``` >结果 ![结果](https://img.tnblog.net/arcimg/hb/1e4643ee8de943e4bf091c68199be0cc.png "结果") <br/> 实现 Fibonacci 数列 (变量) ------------ <br/> >###举例 1,1,2,3,5,8,13... >###编写内容 ```go package fib import "testing" func TestFibList(t *testing.T) { var a int = 1 var b int = 1 t.Log("", b) for i := 0; i < 5; i++ { t.Log(" ", b) tem := a a = b b = tem + a } t.Error("Over") } ``` >###运行结果 ![运行结果](https://img.tnblog.net/arcimg/hb/9c84353565c44d16a6e5acc5b5d8b8a5.png "运行结果") >###简化声明方式 <br/> >简化一 >在一个赋值语句中可以对多个变量进行同时赋值 ```go var ( a int = 1 b int = 1 ) ``` >简化二 >赋值可以进行自动类型判断 ```go a := 1 b := 1 ``` >###全局声明 <br/> >在方法外声明 a 变量 ```go var a int func TestFibList(t *testing.T) { ... a=1 ... } ``` >###简化中间变量赋值 >定义 TestExchange 方法 ```go func TestExchange(t *testing.T) { a := 1 b := 2 a, b = b, a t.Log(a, b) t.Error("Over") } ``` >结果 ![结果](https://img.tnblog.net/arcimg/hb/eb4cbd586b4044d7bc38a3d6ea2febc2.png "结果") <br/> 常量定义 ------------ <br/> >###连续设置连续值 <br/> >const_test.go ```go package const_test import "testing" const ( Monday = 1 + iota Tuesday Wednesday Thursday Friday Saturday Sunday ) func TestConst(t *testing.T) { t.Log(Monday, Tuesday, Wednesday) t.Error("Over") } ``` >结果 ![结果](https://img.tnblog.net/arcimg/hb/2b04293684d04710ad6f0f260470d526.png "结果") >###位运算的常量定义 <br/> >代码定义 ```go const ( Readable = 1 << iota //0001 Writable //0010 Executable //0100 Firable //1000 ) func TestConst(t *testing.T) { t.Log(Readable, Writable, Executable, Firable) t.Error("Over") } ``` >结果 ![结果](https://img.tnblog.net/arcimg/hb/b85af9df1b914e688362b5e5a37d5109.png "结果") >###位运算 <br/> >代码内容 ```go func TestConstantTry(t *testing.T) { a := 7 //0111 t.Log(Readable, Writable, Executable, Firable) t.Log(a&Readable, a&Writable, a&Executable, a&Firable) t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable, a&Firable == Firable) t.Error("Over") } ``` 【注意】:与运算,二进制的最小位都为1则返回后边的结果,否则为0 >结果 ![](https://img.tnblog.net/arcimg/hb/358b21f880854cfc865d573e5bf12fe6.png)
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
168篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
2篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
1篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术