go 常量使用

常量常量介绍

常量使用const定义
常量在定义的时候,必须初始化
常量不能修改
常量只能修饰bool、数值类型(int,float系列)、string类型
语法:const identifier [type] = value
举例说明,看看下面的写法是否正确:

const name = "tom" //ok
const tax float64 =0.8 //ok
const a int // err
const b = 9/3 //ok
var num = 9
const c = num/3 //err num是变量,不确定的,
const c = getVal() // err

package main

import (
	"encoding/json"
	"fmt"
)

func main(){
	const (
		a = iota
		b = iota
		c,
		d = iota, iota
	)
	fmt.Println(a,b,c,d)
	const (
		a1 = iota
		b1 = iota
		c1 = iota
		d1 = iota
	)
	fmt.Println(a1,b1,c1,d1)
	const (
		a2 = iota
		b2
		c2
		d2
	)
	fmt.Println(a2,b2,c2,d2)
}

输出:

0 1 2 2
0 1 2 3
0 1 2 3

posted @   ty1539  阅读(80)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示