前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang: Values, Types and Variables

Golang: Values, Types and Variables

作者头像
Miigon
发布2022-10-27 15:53:15
3110
发布2022-10-27 15:53:15
举报
文章被收录于专栏:Miigon's BlogMiigon's Blog

Variables

Noted that in Go, type lies after variable/function name, which is different from all the other “C-like” languages, eg. C, C++, C#, Java.

Here’s a great article on why the Go declaration syntax is the way it is (with type after names instead of before). In that article Rob Pike has already compared the Go syntax with C syntax so we won’t be doing that here. Check out the article if you are interested.

Declaration

Basic usage

代码语言:javascript
复制
var foo1 int				// variable declaration
var foo2, foo3 int			// multiple variable declaration
var foo2, foo3 int = 1, 2	// ... with initializers

With implicit types

代码语言:javascript
复制
var bar1, bar2 = 3.142, true    // type inferred from the right hand side
// `bar1` -> float64, `bar2` -> bool

// notice how variables with different types can be declared together this way.

Short variable declaration

Variable declaration with implicit types can be substituted for the more elegant Short variable declaration:

代码语言:javascript
复制
bar3 := "hello"				// short variable declaration
// equivalent to:
var bar3 = "hello"

bar4, bar5 := true, "world"	// you can even do this

Notice that “short variable declaration” can be used multiple times for the same variable:

代码语言:javascript
复制
f, err := os.Open(name)
// ...
d, err := f.Stat()

Such code is perfectly legal. However, for the second part, err is only reassigned a new value instead of being declared for a second time.

Note: This only works for variables within the same scope. if a variable with the same name is defined in an outer scope, it would create a new variable in the current scope instead of reassigning the existing outer-scope one.)

Grouped “factored” declaration (like “factored” import)

代码语言:javascript
复制
var (
	foo1 uint = 12
	foo2 int = -3
	isBar bool = true
)

Types

Basic types

代码语言:javascript
复制
bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
	 // represents a Unicode code point

float32 float64

complex64 complex128

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

Type conversion (or the lack of it)

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

代码语言:javascript
复制
i := 42				// int
f := float64(i)		// float64
u := uint(f)		// uint

Unlike in C, in Go assignment between items of different type requires an explicit conversion.

This means the following code:

代码语言:javascript
复制
var i int = 42
var f float32 = i	// error

should not work. Because Go doesn’t allow implicit type conversion.

Constants

Constants are declared like variables, but with the const keyword. Constants can be character, string, boolean, or numeric values. Constants cannot be declared using the := syntax.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Variables
    • Declaration
      • Basic usage
      • With implicit types
      • Short variable declaration
      • Grouped “factored” declaration (like “factored” import)
    • Types
      • Basic types
      • Type conversion (or the lack of it)
    • Constants
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档