Go 语言中包括以下内置基础类型:
布尔型:bool

整型:int int64 int32 int16 int8 uint8(byte) uint16 uint32 uint64 uint

浮点型:float32 float64

复数型:complex64 complex128

字符串:string

字符型:rune

错误型:error

1. bool类型

关键字: bool

可定义为: true 或者 false 或者 逻辑表达式

var bool1 bool = true
var bool2 bool = (2 == 2)

注:不能接受其他类型的赋值,包括(0, 1),也不支持自动或强制类型转换

2. 整型

分为有符号与无符号两种

值范围,如: int8 2^8 -128~127, uint8 0~255, 其他以此类推

int/uint 其值范围与平台有关,所以 int32 != int

支持强制类型转换,注意精度问题

变量2 = 类型(变量1)

3. 浮点型

即含小数点的数据

有两种: float32 float64(默认)

可相互转换,

注:比较大小时最好先确定比较精度, 再比较大小

4. 字符串

声明方式: var str string

值用 "" 或者 `` 包括, `` 可包含多行字符串

字符串的操作与数组相同

与如php等其他语言不同的是,字符串的内容在初始化后,不能被修改,但可重新完全赋值

s := "123"
s[1] = "3" //compile error
$s = "123"; $s[1] = "3"; echo $s; //133

5. 字符型

两种字符类型: 

byte 对应 utf-8

rune 对应 unicode

6. 错误型

在 go 语言中,负责错误信息处理的是 error 接口,也可以使用 errors 包

var e error = errors.New("...")

在 go 语言中,函数支持多个返回值

可用下面的方式来处理错误

res, err := funName(param)
if err != nil {
//处理错误
} else {
//无错误
} func funName(param) (res type, err error) {
if ... {
err = errors.New(...)
return
}
...
return res, nil
}

7. 复数

在此省略,有需要可再了解

note_type_1.go code list

package main

import "fmt"
import "errors" //引入 errors 包 //声明 bool 型全局变量
var (
enable = true
disable = false
) func main() {
//预定义常量 iota
const (
c0 = iota //0
c1 = iota //0+1
c2 //会自动赋上一个定义常量的值或者表达式
)
const (
c3 = iota //0
c4
)
fmt.Println("c0, c1, c2 = ", c0, c1, c2)
fmt.Println("c3, c4 = ", c3, c4) /*
//这种写法编译时会报错,需要 if condition { 在同一行代码中
//missing condition in if statement
//enable == true not used
if enable == true
{
fmt.Println("error")
}
*/
if enable == true {
fmt.Println("enabled = ", enable)
} else {
fmt.Println("enabled = ", disable)
}
/*
//编译时会出现以下错误:
// cannot use 1 (type int) as type bool in assignment
enable = 1;
// cannot convert 1 to type bool
// cannot convert 1 (type int) to type bool
// cannot use 1 (type int) as type bool in assignment
enable = bool(1)
*/ var (
a int8 = 1
b int = 2
)
//invalid operation: a + b (mismatched types int8 and int)
//c := a + b
//需要做类型转换
c := int(a) + b
fmt.Println("c = a + b = ", c) //int32 与 int 是两种不同的类型,但是可用强制类型转换
var d int32
//d = b // cannot use b (type int) as type int32 in assignment
d = int32(b)
fmt.Println("d = ", d) var f1 float32 = 1.23456
fmt.Printf("f1 = %.3f \n", f1) //1.235
f2 := 1.111
//compile error: invalid operation: f1 + f2 (mismatched types float32 and float64
//f3 := f1 + f2
b1 := (float64(f1) == f2)//该比较方式不严谨
if b1 {
fmt.Println("float64(f1) == f2")
} else {
fmt.Println("float64(f1) != f2")
} //用 "" 括起来表示字符串
//字符串的操作与数组一样
var str string = "hello"
fmt.Println("str = ", str)
fmt.Println("str[1] = ", str[1])
fmt.Printf("%c \n", str[1]) // s[i]取第i+1个字符
//str = "hi" //compile ok
//str[0] = 'c' //compile error: cannot assign to str[0] //多行字符串,用 `` 包含
str2 := `
SELECT username, pwd
FROM tb_user
WHERE id = 123456
`
fmt.Println(str2) str3 := " world!"
fmt.Println("str + str3 = ", str + str3) // s1 + s2, 连接字符串 //len(s)返回字符串的长度
fmt.Printf("length of str2 = %d \n", len(str2)) //s[m:n] 返回从m位开始到n结束之间的字符串,m, n可以省略, 此时m为0, n为len(s)
s := "hello"
s = "c" + s[1:]
fmt.Println(s) //cello
fmt.Println(s[:3]) //cel
fmt.Println(s[1:3]) //el
fmt.Println(s[:]) //cello //byte 用 '' 包括字符
//var ch byte = "1" //compile error: cannot use "1" (type string) as type byte in assignment
var ch1 byte = 'a'
fmt.Printf("ch1 = %c \n", ch1) //ch1 = a
fmt.Println(ch1) //97
//rune
var ch2 rune = 'b'
fmt.Printf("ch2 = %c \n", ch2) //ch2 = b
fmt.Println(ch2) //98 //error
err := errors.New("error1")
if err != nil {
//错误处理...
fmt.Println(err)
} var e1 error
if e1 == nil {
fmt.Println("no problem")
} res, e := div(5, 0)
//res, e := div(5, 1)
if e != nil {
//错误处理
fmt.Println(e) //如果div(5, 0), 会输出: error: division by zero
} else {
//正确
fmt.Println("res = ", res) //如果div(5, 1),会输出: res = 5
}
}
//定义函数
func div(a, b int) (res int, e error) {
if b == 0 {
e = errors.New("error: division by zero")
return
}
return a /b , nil
}

运行结果:

go - 内置基础类型的更多相关文章

  1. 【Go入门教程2】内置基础类型(Boolean、数值、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值

    这小节我们将要介绍如何定义变量.常量.Go内置类型以及Go程序设计中的一些技巧. 定义变量 Go语言里面定义变量有多种方式. 使用var关键字是Go最基本的定义变量方式,与C语言不同的是Go把变量类型 ...

  2. 【Go入门教程4】变量(var),常量(const),内置基础类型(Boolean、数值 byte,int,rune、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值

    这小节我们将要介绍如何定义变量.常量.Go 内置类型以及 Go 程序设计中的一些技巧. 定义变量 Go 语言里面定义变量有多种方式. 使用 var 关键字是 Go 最基本的定义变量方式,与 C 语言不 ...

  3. go语言内置基础类型

    1.数值型(Number) 三种:整数型.浮点型和虚数型(有符号整数表示整数范围 -2n-1~2n-1-1:无符号整数表示整数范围 0~2n-1) go内置整型有:uint8, uint16, uin ...

  4. C# 内置的类型转换方法

    C# 提供了下列内置的类型转换方法: 序号 方法 & 描述 1 ToBoolean把类型转换为布尔型. 2 ToByte把类型转换为字节类型. 3 ToChar如果可能的话,把类型转换为单个 ...

  5. C#内置委托类型Func和Action对比及用法

    C#的内置委托类型 Func Action 返回值 有(只有一个Tresult) 无 重载 17个(0参-16参) 17个(0参-16参) 泛型 支持 支持 系统内置 是 是 是否需要声明 否 否 c ...

  6. JS Error 内置异常类型 处理异常 Throw语句

    Exceptional Exception Handling in JavaScript       MDN资料 Anything that can go wrong, will go wrong. ...

  7. python高级(二)—— python内置序列类型

    本文主要内容 序列类型分类: (1)容器序列.扁平序列 (2)可变序列.不可变序列 列表推导式 生成器表达式 元组拆包 切片 排序(list.sort方法和sorted函数) bisect pytho ...

  8. python所有的内置异常类型汇总

    内置异常基类 在 Python 中,所有异常必须为一个派生自 BaseException 的类的实例. 通过子类化创建的两个不相关异常类永远是不等效的,既使它们具有相同的名称. 下列异常主要被用作其他 ...

  9. c++内置变量类型

    1,各种变量占据的内存空间 char:1个字节,也可亦作为0-255的数值参与运算 一般来说,静态存储区的自动赋初值,动态则不自动(貌似也不对,因为非内置变脸的类型,也都调用了默认构造函数进行初始化) ...

随机推荐

  1. Ajax 下拉列表联动显示

    一般处理程序文件 代码 using System;using System.Web;using System.Linq;using System.Data.Linq;using System.Text ...

  2. 提升Delphi编程效率必须使用的快捷键(Delphi2007版本)

    1. [CTRL+空格] [CTRL+SHIFT+空格] 这两个快捷键都是在代码编写过程中用到的,起提示作用,使用频率最高. CTRL+空格: 在当前光标处提示有哪些变量.函数可以使用.这个功能对于无 ...

  3. JavaFX学习之路:详细解释JavaFX架构和框架

    JavaFX 2.0平台是基于Java技术的富client平台.它使应用程序开发人员更加easy的开发和部署跨平台的富互联网应用(RIA).JavaFX 2.0文档包括了JavaFX 2.0所提供的功 ...

  4. 《转》Frameset布局

    前二天在写一个HTML界面,用到了Frameset,主要学习都是在下面的文章里,内容写得很详细,值得推荐大家看下. 网址:http://captaincook.iteye.com/blog/36563 ...

  5. php 用递归实现的无限级别分类

    <?php header("Content-type:text/html; charset=utf-8"); /**  *   * @category contry_cate ...

  6. Virtualbox mouse move in and out and file share with windows

    How to use Virstalbox to share files with Linux and Windows, and to move the mouse in and out Virtua ...

  7. Codeforces Round #252 (Div. 2) B. Valera and Fruits(模拟)

    B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. loj1336(数学)

    传送门:Sigma Function 题意:定义f(n)为n的约数之和,求[1,n]中f值为偶数的数的个数. 分析:由题目给定公式可知,若f(n)为奇数,则相乘的每一项都必须为奇数. 每一项为奇数的条 ...

  9. 联系我们_鲲鹏Web数据抓取 - 专业Web数据采集服务提供者

    联系我们_鲲鹏Web数据抓取 - 专业Web数据采集服务提供者 首页 > 联系我们 我们的联系方式如下: 029 - 82542052(陕西 西安) 13389148466 或 13571845 ...

  10. 项目实践中--Git服务器的搭建与使用指南(转)

    一.前言 Git是一款免费.开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.在平时的项目开发中,我们会使用到Git来进行版本控制. Git的功能特性: 从一般开发者的角度来 ...