大家好,我又来了,接着上一期我们搭建了golang的环境变量后,我们接着看一下编程的基础常识。

众所周知在编程中,我们涉及到一下常用的东西:

  • 数据类型
  • 变量和常量
  • 运算符
  • 语句
  • 数组、集合
  • 方法
  • 资源引入

1、数据类型

在go语言中,我们常见的数据类型有很多,但是值得注意的是,他们都是小写!都是小写!都是小写!

package builtin// bool is the set of boolean values, true and false.
type bool bool// true and false are the two untyped boolean values.
const (true  = 0 == 0 // Untyped bool.false = 0 != 0 // Untyped bool.
)// uint8 is the set of all unsigned 8-bit integers.
// Range: 0 through 255.
type uint8 uint8// uint16 is the set of all unsigned 16-bit integers.
// Range: 0 through 65535.
type uint16 uint16// uint32 is the set of all unsigned 32-bit integers.
// Range: 0 through 4294967295.
type uint32 uint32// uint64 is the set of all unsigned 64-bit integers.
// Range: 0 through 18446744073709551615.
type uint64 uint64// int8 is the set of all signed 8-bit integers.
// Range: -128 through 127.
type int8 int8// int16 is the set of all signed 16-bit integers.
// Range: -32768 through 32767.
type int16 int16// int32 is the set of all signed 32-bit integers.
// Range: -2147483648 through 2147483647.
type int32 int32// int64 is the set of all signed 64-bit integers.
// Range: -9223372036854775808 through 9223372036854775807.
type int64 int64// float32 is the set of all IEEE-754 32-bit floating-point numbers.
type float32 float32// float64 is the set of all IEEE-754 64-bit floating-point numbers.
type float64 float64// complex64 is the set of all complex numbers with float32 real and
// imaginary parts.
type complex64 complex64// complex128 is the set of all complex numbers with float64 real and
// imaginary parts.
type complex128 complex128// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.
type string string// int is a signed integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, int32.
type int int// uint is an unsigned integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, uint32.
type uint uint// uintptr is an integer type that is large enough to hold the bit pattern of
// any pointer.
type uintptr uintptr// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32// iota is a predeclared identifier representing the untyped integer ordinal
// number of the current const specification in a (usually parenthesized)
// const declaration. It is zero-indexed.
const iota = 0 // Untyped int.// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type// Type is here for the purposes of documentation only. It is a stand-in
// for any Go type, but represents the same type for any given function
// invocation.
type Type int// Type1 is here for the purposes of documentation only. It is a stand-in
// for any Go type, but represents the same type for any given function
// invocation.
type Type1 int// IntegerType is here for the purposes of documentation only. It is a stand-in
// for any integer type: int, uint, int8 etc.
type IntegerType int// FloatType is here for the purposes of documentation only. It is a stand-in
// for either float type: float32 or float64.
type FloatType float32// ComplexType is here for the purposes of documentation only. It is a
// stand-in for either complex type: complex64 or complex128.
type ComplexType complex64复制代码

在上面的源码摘录中,我们可以看到go语言完整的数据类型。但是,我们总结下常用的如下:

  • int、uint 整形、无符号整形

    • int8、int16(2字节,-32768~32767)、int32(rune)、int64
    • uint8、uint16(2字节,0~65535)、uint32、uint64
  • string 字符串(原生utf-8编码支持)
  • bool 布尔型
  • float 浮点型
    • float32、float64 (4/8字节,精确到小数后7/15位)
  • byte 1字节 uint8的别名
  • nil 空类型
  • complex64、complex128 复数类型(实数、虚数),8字节、16字节
  • uintptr 足够大的位模式保存指针的整数类型(uintptr is an integer type that is large enough to hold the bit pattern of any pointer.)

同样的,还有一些派生数据类型(我个人喜欢称之为扩展数据类型或者是包装数据类型),基本如下:

  • 指针类型(Pointer)
  • 数组类型
  • 结构化类型(struct)
  • Channel 类型
  • 函数类型
  • 切片类型
  • 接口类型(interface)
  • map类型

当然这些扩展数据类型我们将在以后一一阐述。

go 1.9版本对于数字类型,无需定义int及float,系统会自动识别。

附赠demo小案例(完成变量的申明和使用,以及注意事项)

package main//导包
import "fmt"//分组申明变量
var (personA = "小王"personC = "小张"ageA    = 18
)func main() {//快捷申明变量 省却var标识符mName := "程先生"//var 标明数据是变量var mSex = "汉子"fmt.Println(mName + ",哎哟我去,你是" + mSex)var a = 1.5var b = 2fmt.Println(a, " ====我是分隔符==== ", b)fmt.Println(personA, " ====我是分隔符==== ", personC, " ====我是分隔符==== ", ageA)
}
复制代码

当然,运行结果如下:

程先生,哎哟我去,你是汉子
1.5  ====我是分隔符====  2
小王  ====我是分隔符====  小张  ====我是分隔符====  18
复制代码

在上面的代码中,有下面一些操作

  • 必须申明包,且主程序包为:main
  • 入口函数为:main,且没有任何参数
  • var为变量申明标识符
  • 变量可以分组申明(导包可以分组吗?)
  • 可以采用快捷申明变量 := ,注意:仅仅能在函数中使用
  • 变量的基本数据类型无需申明,系统会自动推导
    • 当然手动申明变量类型如: var aa string = "糍粑"
  • 所有的变量都是使用过的(未使用的变量会产生编译异常)
  • java中输出语句基本数据类型和string可以通过“+”连接,但是go中不允许这种骚操作
  • 变量可以即时申请
  • 无需“;”结尾,换行既是语句的结束
  • 虽然允许多变量一行赋值,但是不推荐如此做,所以也未举例

常量

常量是一个简单值的标识符,在程序运行时,不会被修改的量。 常量中的数据类型只可以是布尔型、数字型(整数型、浮点型和复数)和字符串型。

常量定义格式: const identifier [type] = value

如下:

  • 显式类型定义: const b string = "abc"
  • 隐式类型定义: const b = "abc"
  • 多个相同类型的声明可以简写为: const c_name1, c_name2 = value1, value2 (当然,我一样不推荐这样定义,降低了可读性)

那么我们试一试小demo

//在main方法中执行下面代码段const PI = 3.14r := 4fmt.Println("圆的面积为:", PI*r*r)
复制代码

在我们运行后代码报错如下:

# command-line-arguments./demo001.go:26:38: constant 3.14 truncated to integer复制代码

上面代码提示PI被截断为整数,但是程序却没能成功运行,是不是go不支持数据类型自动转换呢?

我们这时候考虑一下把常量显示申明为float32试一试:

 const PI float32 = 3.14r := 4fmt.Println("圆的面积为:", PI*r*r)
复制代码

这次更加凄惨,代码甚至不能通过编译,在“PI *r *r”这里提示类型匹配错误!emmmm····

我们只能选择把r申明为float试试,如下:

  const PI = 3.14r := 4.0fmt.Println("圆的面积为:", PI*r*r)
复制代码

恭喜,本次编译通过,且有输出内容,如下:

圆的面积为: 50.24
复制代码

当然如下代码也能成功运行:

    const PI float32 = 3.14var r float32 = 4.0fmt.Println("圆的面积为:", PI*r*r)
复制代码

这说明,go是一个严格强调数据类型匹配的语言。

当然,前面我们看到变量分组申明,那么这里我们常量一样可以分组申明:

const (OK = 0ERROR = 1EMPUTY = -1
)
复制代码

常量申明中的 iota

ota,特殊常量,可以认为是一个可以被编译器修改的常量。 在每一个const关键字出现时,被重置为0,然后再下一个const出现之前,每出现一次iota,其所代表的数字会自动增加1。

我们来枚举一些常量:

const (a = iotab = iotac = iota
)//第一个 iota 等于 0,每当 iota 在新的一行被使用时,它的值都会自动加 1;所以 a=0, b=1, c=2 可以简写为如下形式:const (a = iotabc
)
复制代码

当然我们也可以看看别人教程(菜鸟教程-> Go语言常量)所书写的例子:

package mainimport "fmt"func main() {const (a = iota   //0b          //1c          //2d = "ha"   //独立值,iota += 1e          //"ha"   iota += 1f = 100    //iota +=1g          //100  iota +=1h = iota   //7,恢复计数i          //8)fmt.Println(a,b,c,d,e,f,g,h,i)
}
复制代码

运行结果如下:

0 1 2 ha ha 100 100 7 8
复制代码

上面的例子,说实话,有一点绕,同时我也不建议在实际开发中写出这种骚操作,毕竟这样写可能会被队友打死。

当然iota也可以加入到移位运算中:

package mainimport "fmt"
const (//第一个iota默认值是0,左移位1,则不变i=1<<iota//左移位 0+1,3的二进制是0011,左移位一次就是0110,十进制结果为6j=3<<iota//这里实际代码是: k = 3 << iota ,按照上面的推算则是左移位1+1,下面的l同理kl
)func main() {fmt.Println("i=",i)fmt.Println("j=",j)fmt.Println("k=",k)fmt.Println("l=",l)
}
复制代码

输出结果:

i= 1
j= 6
k= 12
l= 24
复制代码

总结

变量的申明:

  • var name [type] = value
  • 方法内 name := value
  • 变量组申明 var (name = value)

常量的申明:

  • const name [type] = value
  • 常量组申明:const (name = value)

其他:

  • 内置函数
  • 变量常量数据类型自推导
  • 严格的数据类型限制

golang常用手册:数据类型、变量和常量相关推荐

  1. c语言基本数据类型常量,C语言基础学习基本数据类型-变量和常量

    变量和常量 什么是变量和常量?有些数据在程序运行前就预先设定,并在运行过程中不发生变化,称之为常量:有些数据在程序运行中可能发生变化或被赋值,称之为变量. 使用变量前必须先声明一个变量.变量定义的语法 ...

  2. golang常用手册:数组、变量作用域、函数

    学习是一个趁热打铁的过程,高效的学习能提高大脑反应.所以我们接着了解go语言的数组.变量作用域.函数. 数组 和其他语言申明类似:var variable_name [SIZE] variable_t ...

  3. python常量基本类型有哪些_【Python③】python基本数据类型,变量和常量

    基本数据类型 Python中,能直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,包括负整数,程序中的写法和数学上的一样,例如:6,-666,8888-- 计算机使用二进制,所 ...

  4. Go语言基础之1--标识符、关键字、变量和常量、数据类型、Go的基本程序结构、Golang的特性...

    一.前言 当我们项目较为简单时,我们在src目录下新建一个该项目目录,里面存放源码文件即可,见下图: 当我们一个项目较为复杂时,我们可以在src目录下新建一个该项目目录,在针对该项目不同模块创建不同目 ...

  5. 列举python中常用的数据类型_Python基础知识 变量和简单数据类型

    在本章节中,将介绍Python程序中会使用到的各种数据类型,以及如何在程序中使用变量来表示这些数据.其中用到的一些例子均来自<Python编程从入门到实践 第2版>. 一.变量 1. 变量 ...

  6. [转载] python定义整型常量_Python笔记——数据类型、变量和常量

    参考链接: Python变量,常量和文字 数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频 ...

  7. 02_星仔带你学Java之变量、常量、字面量、数据类型

    资料和代码存放地址:<华星详谈-学习中心>.开源项目持续更新中.     大家好,我是星仔.本博客收录于华星详谈-学习中心.本学习中心收集了Java整个技术体系的所有技术要点.每篇博客后面 ...

  8. matlab变量与常量、数据类型

    变量与常量 变量是任何程序设计语言的基本元素之一. Matlab 并不要求对所使用的变量进行事先声明,也不需要指定变量类型,Matlab 会自动根据所赋予变量的值或对变量所进行的操作来确定变量的类型. ...

  9. Python day1 变量、常量、注释基础数据类型初始以及用户输入input

    CPU:计算机的运算和计算中心,相当于人类的大脑. 内存:暂时存储数据,临时加载数据应用程序,速度快,断电即消失,造价很高. 硬盘:磁盘,长期存储数据,速度较慢,造价相对较低. 操作系统:一个软件,连 ...

最新文章

  1. 位运算+取某一位+java_Java位运算小节
  2. [分享]写出健壮的Bash脚本
  3. centos下加入mysql环境变量
  4. 您如何计算字符串中字符串(实际上是字符)的出现?
  5. C语言实现的ABCI
  6. 超干货 | 一线从业者实践案例大分享:To B行业如何做增长
  7. C++继承中关于子类构造函数的写法
  8. 双显示器设置:如何设置一台电脑两个显示器
  9. html图片自动循环轮播图,js实现图片无缝循环轮播
  10. Python笔记-安装python虚拟环境及配置opencv及通过opencv识别颜色
  11. 从计算机系统结构的发展和演变看,近代计算机是以,西南民族大学计算机系统结构试卷B有答案.doc...
  12. 感知器分类模型回顾与python实现
  13. win10+Ubuntu17.10双系统安装(惠普台式机)
  14. nslookup java_使用JAVA实现nslookup命令
  15. viewModel生命周期
  16. HBuilderx 配置多环境发行
  17. 男人:别光着上身睡觉
  18. Android调用高德地图直接导航的简单实例
  19. SpringBoot开源文档型知识库系统,源代码分享
  20. 最受欢迎的webgl 3d引擎

热门文章

  1. 工作实战: 让 SQL 飞起来
  2. 可有可无的Mysql工作技巧 3 -- 工作中用到的理论范式,工具,建模经验
  3. 第二章--Mysql分区表
  4. 设计模式--观察者模式--Java实现
  5. RocketMQ如何保证消息不丢失(消息可靠性)
  6. 使用TargetSources
  7. Linux的网卡由eth0变成了eth1,如何修复
  8. Go Web编程--应用数据库
  9. 云计算中的Iaas,Paas和Saas
  10. 关于SpringBoot场景启动器