Go语言编程入门:映射、常量、指针

2023-02-0410:47:47编程语言入门到精通Comments835 views字数 498阅读模式

映射

映射的增删改查

映射也被称作map或字典,存储的是键值对类型的关系。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/30706.html

示例代码:映射的增删改查文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/30706.html

package main

import "fmt"

func main() {
	// 创建
	m := make(map[string]int)

	// 新增
	m["a"] = 1
	m["b"] = 2
	fmt.Println(m)

	// 删除
	delete(m, "a")
	fmt.Println(m)

	// 修改
	m["b"] = 11
	fmt.Println(m)

	// 查询
	fmt.Println(m["b"])
}

常量

常量的基本定义

示例:定义常量文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/30706.html

package main

import "fmt"

func main() {
	const PI = 3.14
	fmt.Println(PI)
}

常量的自增

示例:使用iota让常量自增文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/30706.html

package main

import "fmt"

const (
	a = iota
	b
	c
)

func main() {
	fmt.Println(a, b, c)
}

指针

指针的基本用法

示例:指针的获取和使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/30706.html

package main

import "fmt"

func main() {
	var a = 33

	// 获取指针
	pA := &a

	// 通过指针修的值
	*pA = 34

	// 查看指针的值
	fmt.Println(*pA)
}
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/30706.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/30706.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定