The expression T(v) converts the value v to the type T.

Some numeric conversions:

var i int = 42
var f float64 = float64(i)
var u uint = uint(f)

Or, put more simply:

i := 42
f := float64(i)
u := uint(f)

Unlike in C, in Go assignment between items of different type requires an explicit conversion. Try removing the float64 or int conversions in the example and see what happens.

package main 

import (
"fmt"
"math"
) func main() {
var x, y int = ,
var f float64 = math.Sqrt(float64(x*x + y*y))
var z int = int(f)
fmt.Println(x, y, z)
}

A Tour of Go Type conversions的更多相关文章

  1. Type conversions in C++类型转换

    ###Implicit conversions隐式转换* 可以在基本类型之间自由转换:* 可以把任何类型的pointer转换为void pointer:* 可以将子类pointer转换为基类point ...

  2. [Compose] 20. Principled type conversions with Natural Transformations

    We learn what a natural transformation is and see the laws it must obey. We will see how a natural t ...

  3. 条款24:若所有参数皆需要类型转换,请为此采用non-member函数(Declare non-member functions when type conversions should apply to all parameters)

    NOTE: 1.如果你需要为某个函数的所有参数(包括this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.

  4. C++: Type conversions

    1.static_cast     static_cast可以转换相关联的类,可以从子类转换成父类.也能从父类转向子类,但是如果转换的父类指针(或者父类引用)所指向的对象是完整的,那么是没有问题:但是 ...

  5. 后端程序员之路 51、A Tour of Go-1

    # A Tour of Go    - go get golang.org/x/tour/gotour    - https://tour.golang.org/    # welcome    - ...

  6. Type Systems

    This section deals with more theoretical aspects of types. A type system is a set of rules used by a ...

  7. Type system

    Type system[edit] Main articles: Data type, Type system, and Type safety A type system defines how a ...

  8. Type system-Type checking

    类型系统的属性: 1.结构属性: 2.规则属性:类型系统定义了一套规则(内部数据的访问规则.函数的访问规则.类型的比较与转化规则),以供编译和运行时进行检查. In programming langu ...

  9. The Go Programming Language. Notes.

    Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...

随机推荐

  1. js常见事件

    1.onblur:(使用在表单元素中,当元素失去焦点的时候执行) 2.onchange:(使用在表单元素中,当某些东西改变是执行) 3.onclick:(鼠标点击一个元素时执行) 4.ondblcli ...

  2. 安全测试常见的10个问题 ZT

    1, 问题:没有被验证的输入     测试方法:     数据类型(字符串,整型,实数,等)    允许的字符集    最小和最大的长度    是否允许空输入    参数是否是必须的    重复是否允 ...

  3. oracle删除表的方法

    1. 删除oracle表中的所有数据而不删除表: 语法: TRUNCATE TABLE table_name; 使用这条语句只是删除表中的全部数据,不是删除表,这种方式也叫做截断表,这种方式比使用de ...

  4. BZOJ 3983 Takeover Wars 解题报告

    我猜了一个结论,能合并就合并,到了必须要敌对交易的时候才进行敌对交易. 然后合并的话,肯定是拿最大的两个去合并. 至于敌对交易,肯定是干掉对方最大的公司才是有意义的. 于是各种分类讨论...看代码好了 ...

  5. [转载]C# HashTable 遍历与排序

    private void Form1_Load(object sender, EventArgs e) { Hashtable ht = new Hashtable(); ht.Add("j ...

  6. How to Cracked Sublime Text 3 Build 3065 in Ubuntu (Linux)

    整理自How to Cracked Sublime Text 3 Build 3065 in Ubuntu (Linux) Sublime Text 3 Build 3065 Release Date ...

  7. python image模块

    Image 模块 Image 模块提供了同名的类用来表示PIL的图像.Image模块还提供了许多工厂(factory)函数,包块从文件加载图像的函数,以及创建新图像的函数.    例子  下面的脚本加 ...

  8. 李洪强漫谈iOS开发[C语言-015]-变量的使用

  9. 【HDOJ】3652 B-number

    终于自己写出来一道数位DP啊.继续训练DP. /* 3652 */ #include <iostream> #include <sstream> #include <st ...

  10. 【转】NI语法 JNI参考 JNI函数大全

    原文网址:http://blog.sina.com.cn/s/blog_5de73d0b0101chk1.html 一.对照表 Java类型    本地类型         描述boolean     ...