26

【Go从学会到学废】(三) 变量和常量

 4 years ago
source link: https://studygolang.com/articles/31176
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Go声明变量方法:

Go 语言变量名由字母、数字、下划线组成,其中首个字符不能为数字。

声明变量的一般形式是使用 var 关键字:

第一种声明变量方法:

var identifier type
var identifier1,identifier2,identifier3 type
var s0 string
s0 = "Hello"
var s1 string = "Hello"
var s2, s3 string = "Hello", "World"
var i1 int = 10
var i2, i3, i4 int = 1, 2, 3
var b1 bool = true
var b2, b3 bool = true, false

也可以这样

var (
    s0 string
    s1         string
    s2, s3     string
    i1         int
    i2, i3, i4 int
    b1         bool
    b2, b3     bool
)

==注:==

  • ==声明了变量一定要使用==
  • ==与 C/C++,Java 不同的是, GO 类型在前,变量名在后==

第二种声明变量方法:

根据值自行判定变量类型

var s1 = "Hello"
var s2, s3 = "Hello", "World"
var i1 = 10
var i2, i3, i4 = 1, 2, 3
var b1 = true
var b2, b3 = true, false

第三种声明变量方法:

省略 var , 使用 :=

s1 := "Hello"
s2, s3 := "Hello", "World"
i1 := 10
i2, i3, i4 := 1, 2, 3
b1 := true
b2, b3 := true, false
  • ==左侧如果不是声明新的变量,就会报错==

  • ==如果在相同的代码块中,我们不可以再次对于相同名称的变量使用初始化声明==

  • 使用起来简洁,高效

    var a int 
    a := 1

    s := "Hello"
    s := "World"
    var s

    都是错的

匿名变量

可以用 _ 作为匿名变量,意味着 只写 或者 占位符

_, i1 := 10, 9
s1, _, _ := "Hello", "World",""

Go声明变量方法:

声明变量的一般形式是使用 const关键字:

const s1 string = "Hello"
const s2 = "World"
const b1 bool = true
const b2 = false
const i1 int = 1
const i2 = 0

或者

const (
    s1 string = "Hello"
    s2        = "World"
    b1 bool   = true
    b2        = false
    i1 int    = 1
    i2        = 0
)

==注:==

  • ==常量声明必须初始化,但不一定要使用==

itoa

const 内的 iota是golang语言的常量计数器,只能在常量的表达式中使用,,即const内。iota==在const关键字出现时将被重置为0==(const内部的第一行之前),const中==每新增一行常量==声明将使iota计数一次。可以参照行号理解,也就是说将iota理解为const语句块中的行索引。类似于枚举

const a = iota // a=0
const b = iota //b=0
const (
    c    = iota               //c=0
    d                         //d=1
    e, f = iota + 0, iota + 0 //e=2,f=2

)

有疑问加站长微信联系

iiUfA3j.png!mobile

Recommend

  • 78
    • studygolang.com 6 years ago
    • Cache

    05-Go语言常量和变量

    Go语言数据类型 Go语言本质是用C语言编写的一套高级开发语言, 所以Go语言中的数据类型大部分都是由C语言演变而来的 C语言数据类型

  • 39
    • studygolang.com 5 years ago
    • Cache

    Golang:常量和变量

    标识符与关键字 标识符 由数字,字母,下划线组成,只能以字母和下划线开头. 关键字 关键字和保留字不能用作变量名. 25个关键字: break default func interface...

  • 40
    • blog.huangz.me 5 years ago
    • Cache

    Go 的变量与常量

    Note 本文摘录自即将出版的《Go语言趣学指南》, 请访问 gpwgcn.com 以获取更多相关信息。 ...

  • 8
    • studygolang.com 5 years ago
    • Cache

    3.变量和常量

    Go语言基础之变量和常量 | Golang 变量和常量是编程中必不可少的部分,也是很好理解的一部分。 标...

  • 39
    • blog.huangz.me 5 years ago
    • Cache

    Go 语言中的变量与常量

    Note 本文摘录自《Go语言趣学指南》第 2 章, 请访问 gpwgcn.com 以获取更多相关信息。

  • 16

    1、新建 Hello.go package main import "fmt" func main() { fmt.Println("Hello World") } 2、运行 你可以输入 go run Hello.go 直接运行

  • 13

    Go 语言中的变量与常量¶ 本文摘录自《Go语言趣学指南》第 2 章, 请访问 gpwgcn.com 以获取更多相关信息。

  • 13
    • blog.danthought.com 4 years ago
    • Cache

    Swift 变量和常量

    Swift 变量和常量 从这篇文章,你将学习到如何使用 Swift 变量和常量,和其他任何编程语言一样,编程所使用的变量和常量都是要指定类型的,所以也会谈到类型相关的问题,最后会说一下元组 Tuples,让我们开始吧。

  • 7
    • blog.huangz.me 4 years ago
    • Cache

    Go 的变量与常量 — blog.huangz.me

    Go 的变量与常量¶ 本文摘录自即将出版的《Go语言趣学指南》, 请访问 gpwgcn.com 以获取更多相关信息。

  • 13

    同时搞定Android和iOS的Dart语言(2):变量与常量_李宁的极客世界bgJBm&nku$q$-CSDN博客1. 定义和使用变量 尽管Dart是静态语言,但仍然拥有动态特性。所以定义变量也有如下2种方式。 使用数据类型定义变量使用var...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK