Go语言学习之数据类型以及类型转换(The way to go)
生命不止,继续go go go
介绍来go中的变量和常量,今天介绍一下go中的基本类型。
可以分为四大类,现在一点点道来。
Boolean Types
布尔类型,不用过多介绍来吧,就是true和false。
Numeric Types
数值类型包括整型和浮点型:
integer types
floating point values
others
string types
A string type represents the set of string values. Its value is a sequence of bytes. Strings are immutable types that is once created, it is not possible to change the contents of a string. The predeclared string type is string.
Derived types
包括:
Array types 数组
Structure types 结构体
Union types 联合
Slice types 切片,引用类型
Interface types 接口
Map types 字典,引用类型
Channel Types 通道,引用类型
下面就slice map channel进行简要介绍。
首先需要明确,以上三者都是引用类型。而引用类型必须使用make函数创建,编译器会将make转化为目标类型专用的创建函数。
slice
声明:
var numbers []int /* a slice of unspecified size */
numbers = make([]int,5,5) /* a slice of length 5 and capacity 5*/
len() and cap() 函数:
len() :returns the elements presents in the slice
cap():returns the capacity of slice as how many elements it can be accomodate
package main
import "fmt"
func main() {
var numbers = make([]int,3,5)
printSlice(numbers)
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
输出:len=3 cap=5 slice=[0 0 0]
sub-slice:
package main
import "fmt"
func main() {
/* create a slice */
numbers := []int{0,1,2,3,4,5,6,7,8}
printSlice(numbers)
/* print the original slice */
fmt.Println("numbers ==", numbers)
/* print the sub slice starting from index 1(included) to index 4(excluded)*/
fmt.Println("numbers[1:4] ==", numbers[1:4])
/* missing lower bound implies 0*/
fmt.Println("numbers[:3] ==", numbers[:3])
/* missing upper bound implies len(s)*/
fmt.Println("numbers[4:] ==", numbers[4:])
numbers1 := make([]int,0,5)
printSlice(numbers1)
/* print the sub slice starting from index 0(included) to index 2(excluded) */
number2 := numbers[:2]
printSlice(number2)
/* print the sub slice starting from index 2(included) to index 5(excluded) */
number3 := numbers[2:5]
printSlice(number3)
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
输出:
len=9 cap=9 slice=[0 1 2 3 4 5 6 7 8]
numbers == [0 1 2 3 4 5 6 7 8]
numbers[1:4] == [1 2 3]
numbers[:3] == [0 1 2]
numbers[4:] == [4 5 6 7 8]
len=0 cap=5 slice=[]
len=2 cap=9 slice=[0 1]
len=3 cap=7 slice=[2 3 4]
append() 和 copy()函数:
package main
import "fmt"
func main() {
var numbers []int
printSlice(numbers)
/* append allows nil slice */
numbers = append(numbers, 0)
printSlice(numbers)
/* add one element to slice*/
numbers = append(numbers, 1)
printSlice(numbers)
/* add more than one element at a time*/
numbers = append(numbers, 2,3,4)
printSlice(numbers)
/* create a slice numbers1 with double the capacity of earlier slice*/
numbers1 := make([]int, len(numbers), (cap(numbers))*2)
/* copy content of numbers to numbers1 */
copy(numbers1,numbers)
printSlice(numbers1)
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
输出:
len=0 cap=0 slice=[]
len=1 cap=2 slice=[0]
len=2 cap=2 slice=[0 1]
len=5 cap=8 slice=[0 1 2 3 4]
len=5 cap=16 slice=[0 1 2 3 4]
map
声明:
/* declare a variable, by default map will be nil*/
var map_variable map[key_data_type]value_data_type
/* define the map as nil map can not be assigned any value*/
map_variable = make(map[key_data_type]value_data_type)
使用:
package main
import "fmt"
func main() {
var countryCapitalMap map[string]string
/* create a map*/
countryCapitalMap = make(map[string]string)
/* insert key-value pairs in the map*/
countryCapitalMap["France"] = "Paris"
countryCapitalMap["Italy"] = "Rome"
countryCapitalMap["Japan"] = "Tokyo"
countryCapitalMap["India"] = "New Delhi"
/* print map using keys*/
for country := range countryCapitalMap {
fmt.Println("Capital of",country,"is",countryCapitalMap[country])
}
/* test if entry is present in the map or not*/
capital, ok := countryCapitalMap["United States"]
/* if ok is true, entry is present otherwise entry is absent*/
if(ok){
fmt.Println("Capital of United States is", capital)
}else {
fmt.Println("Capital of United States is not present")
}
}
channel
Channel是Go中的一个核心类型,你可以把它看成一个管道,通过它并发核心单元就可以发送或者接收数据进行通讯(communication)。
以后会进行详细的介绍。
自定义类型
使用关键字type定义自定义类型:
type flag byte
类型转换
go必须使用显式类型转换!!!!
a := 1
b := byte(a)
不能将非bool类型当true/false使用!!!!
a := 1
var b bool = a
错误:cannot use a (type int) as type bool in assignment
a := 1
if a{
}
错误:non-bool a (type int) used as if condition
Go语言学习之数据类型以及类型转换(The way to go)的更多相关文章
- Go语言学习之数据类型
### Go语言学习之数据类型 数据类型的转换 1.Go语言不允许隐式类型转换(显示转换才可以) 2.别名和原有类型也不能进行隐式类型转换 例子: func TestImplicit(t *testi ...
- GO语言学习——基本数据类型字符串
字符串 Go语言中的字符串以原生数据类型出现. Go 语言里的字符串的内部实现使用UTF-8编码. 字符串的值为双引号(")中的内容,可以在Go语言的源码中直接添加非ASCII码字符 GO语 ...
- python语言学习7——数据类型和变量
整数 python可以处理任意大小的整数,包括负整数,在程序中的表示方法和数学上的写法一样 计算机由于使用二进制,有时候采用十六进制表示整数比较方便,十六进制数用0x前缀 浮点数 简单的小数就直接用小 ...
- java学习笔记——数据类型及类型转换
数据类型分为: 1.引用类型(字符型); 2.基本数据类型(数值型); 以下为基本数据类型介绍(括号内的数字表示该类型所占据的字节数) a.整型 byte(8) short(16) int(3 ...
- python自动化--语言基础一数据类型及类型转换
Python中核心的数据类型有哪些?变量(数字.字符串.元组.列表.字典) 什么是数据的不可变性?哪些数据类型具有不可变性数据的不可变是指数据不可更改,比如: a = () #定义元组 #a[]= # ...
- GO语言学习——基本数据类型——整型、浮点型、复数、布尔值、fmt占位符
基本数据类型 整型 整型分为以下两个大类: 按长度分为:int8.int16.int32.int64 对应的无符号整型:uint8.uint16.uint32.uint64 其中,uint8就是我们熟 ...
- C语言学习笔记之数据类型转换
1.整数与整数相除,结果也为整数 2.不同类型的运算,精度低的向精度高的转化 整数与浮点数运算就是个很好的例子,只要有一方为浮点数,结果也是浮点数,这也体现出精度低向精度高转化 3.在赋值运算中,等号 ...
- C语言学习关于数据类型的一些知识点(初学者)
1.整型常量的的前缀:(1)十进制常数无前缀. (2)八进制常数前缀为0,八进制常数无符号. (3)十六进制常数前缀为0X或0x. 注:在程序中是根据前缀来区分各种进制数的.因此在书写常数时不要把前缀 ...
- java与.net比较学习系列(3) 基本数据类型和类型转换
在Java中,数据类型分为两类,一类是基本数据类型,另外一类是引用类型. 而在C#中,数据类型分为三类,分别是基元类型,值类型和引用类型.其中基元类型是.net framework框架中预定义的类型, ...
随机推荐
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- CentOS中Apache虚拟主机(virtualHost)设置在/home目录下的若干问题
在Ubuntu中安装LAMP是非常简单的意见事情.但是在CentOS中却遇到了很多问题. 首先是CentOS中必须手动配置iptables,把80端口开放出来,不然,是访问不到的,开放80端口在/et ...
- 分享申请IDP账号的过程,包含duns申请的分享
本文转载至 http://www.cocoachina.com/ios/20140325/8038.html 5月份接到公司要申请开发者账号的任务,就一直在各个论坛找申请的流程,但都是一些09年10年 ...
- sql中between and 用法
SQL中 between and是包括边界值的,not between不包括边界值,不过如果使用between and 限定日期需要注意,如果and后的日期是到天的,那么默认为00:00:00 例如: ...
- Hbase的shell命令学习
在学习Hbase的shell命令,之前先得了解如何进入hbase的shell命令行,通过执行如下简单的命令回车后进入hbase的shell命令行界面 hbase shell 进入hbase命令行后,执 ...
- 做好准备,让你的短信应用迎接Android 4.4(KitKat)
Android团队通过Android开发博客透漏今年会放出Android 4.4 (KitKat) ,同时更新了 SMS 的部分API.博客上讲只有default SMS app才能对短信数据库有写权 ...
- HDU 5892 Resident Evil
题目链接:传送门 题目大意:有50种动物,给你n*n的矩阵,m次操作,P代表加入操作,在左上角 x1,y1 到右下角 x2,y2,的矩形范围内加入 种类为x,数量为y的动物. Q代表询问操作,在左上角 ...
- 【BZOJ4675】点对游戏 树分治+期望
[BZOJ4675]点对游戏 Description 桑尼.露娜和斯塔在玩点对游戏,这个游戏在一棵节点数为n的树上进行. 桑尼.露娜和斯塔三人轮流从树上所有未被占有的节点中选取一点,归为己有,轮流顺序 ...
- java汉字转拼音的工具类
import com.google.common.base.Strings;import net.sourceforge.pinyin4j.PinyinHelper;import net.source ...
- 160705、总结:commons-codec.jar中常用方法
一.Base64编码和解码import org.apache.commons.codec.EncoderException;import org.apache.commons.codec.binary ...