Golang M 2023 4 topic
Posted 知其黑、受其白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang M 2023 4 topic相关的知识,希望对你有一定的参考价值。
阅读目录
- topic
topic
数组求和
package main
import "fmt"
func main()
var i [10]int = [10]int8, 3, 2, 9, 4, 6, 10, 0, 7, 11
j := sumArray(i)
fmt.Println(i)
fmt.Println(j)
func sumArray(a [10]int) int
var sum int = 0
// 第一种遍历方式
for i := 0; i < len(a); i++
sum = sum + a[i]
// 第二种遍历方式
// for _, v := range a
// sum = sum + v
//
return sum
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
[8 3 2 9 4 6 10 0 7 11]
60
PS E:\\TEXT\\test_go\\one>
func main()
var b [10]int
for i := 0; i < len(b); i++
b[i] = i
sum := sumArray(b)
fmt.Printf("sum=%d", sum)
// sum=45
package main
import (
"fmt"
"math/rand"
)
func main()
var b [10]int
for i := 0; i < len(b); i++
// 随机值
b[i] = rand.Intn(1000)
sum := sumArray(b)
fmt.Printf("sum=%d", sum)
// sum=45
func main()
// 初始化随机种子
rand.Seed(time.Now().Unix())
var b [10]int
for i := 0; i < len(b); i++
// 产出一个 0 到 999 的随机种子
b[i] = rand.Intn(1000)
// 产生一个 0 到 int 最大值的随机数
// b[i] = rand.Int()
sum := sumArray(b)
fmt.Printf("sum=%d", sum)
// sum=45
二维数组以及遍历
package main
import "fmt"
func main()
testArray()
func testArray()
a := [5]int3: 100, 4: 300
for i := 0; i < len(a); i++
fmt.Printf("a[%d]=%d\\n", i, a[i])
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
a[0]=0
a[1]=0
a[2]=0
a[3]=100
a[4]=300
PS E:\\TEXT\\test_go\\one>
package main
import "fmt"
func main()
testArray()
func testArray()
var a [3][2]int
a[0][0] = 10
a[0][1] = 20
a[1][0] = 30
a[1][1] = 40
a[2][0] = 50
a[2][1] = 60
// fmt.Println(a) [[10 20] [30 40] [50 60]]
for i := 0; i < 3; i++
for j := 0; j < 2; j++
fmt.Printf("%d ", a[i][j])
fmt.Println()
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
10 20
30 40
50 60
PS E:\\TEXT\\test_go\\one>
第二种遍历方式
func testArray()
var a [3][2]int
a[0][0] = 10
a[0][1] = 20
a[1][0] = 30
a[1][1] = 40
a[2][0] = 50
a[2][1] = 60
// fmt.Println(a) [[10 20] [30 40] [50 60]]
for i, v := range a
for j, v2 := range v
fmt.Printf("(%d,%d)=%d ", i, j, v2)
fmt.Println()
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
(0,0)=10 (0,1)=20
(1,0)=30 (1,1)=40
(2,0)=50 (2,1)=60
PS E:\\TEXT\\test_go\\one>
找出数组两个元素之和等于 8
找出数组中和为给给定值的两个元素的下标,比如数组:[1,3,5,8,7],找出两个元素之和等于 8 的下标分别是(0,4)和(1,2)。
package main
import "fmt"
func main()
b := [...]int1, 3, 5, 8, 7
TwoSum(b, 8)
func TwoSum(a [5]int, target int)
for i := 0; i < len(a); i++
other := target - a[i]
for j := i + 1; j < len(a); j++
if a[j] == other
fmt.Printf("(%d,%d)\\n", i, j)
切片追加切片
package main
import "fmt"
func main()
testAppend()
func testAppend()
var a []int = []int1, 3, 4
var b []int = []int4, 5, 6
a = append(a, 23, 34, 45)
fmt.Printf("a=%v\\n", a)
a = append(a, b...)
fmt.Printf("a=%v\\n", a)
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
a=[1 3 4 23 34 45]
a=[1 3 4 23 34 45 4 5 6]
PS E:\\TEXT\\test_go\\one>
切片传参
package main
import "fmt"
func main()
nos := []int8, 7, 6
fmt.Println("slice before function call", nos)
subtactOne(nos)
// nos 修改生效了,说明切片是引用类型
fmt.Println("slice after function call", nos)
func subtactOne(numbers []int)
for i := range numbers
numbers[i] -= 2
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
slice before function call [8 7 6]
slice after function call [6 5 4]
PS E:\\TEXT\\test_go\\one>
数组获取成切片
package main
import "fmt"
func main()
var a [10]int = [10]int1, 3, 3, 4, 5, 5, 8
sum := sumArray(a[:])
fmt.Println("sum:", sum)
func sumArray(a []int) int
var sum int = 0
for _, v := range a
sum = sum + v
return sum
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
sum: 29
PS E:\\TEXT\\test_go\\one>
make([]string, 5, 10)
package main
import "fmt"
func main()
var sa = make([]string, 5, 10)
fmt.Println("a:", sa)
for i := 0; i < 10; i++
sa = append(sa, fmt.Sprintf("%v", i))
fmt.Println("a:", sa)
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
a: [ ]
a: [ 0 1 2 3 4 5 6 7 8 9]
PS E:\\TEXT\\test_go\\one>
sort 排序
正序 int、string、float64
package main
import (
"fmt"
"sort"
)
func main()
var a [6]int = [6]int5, 4, 3, 2, 1, 0
sort.Ints(a[:])
fmt.Println("a:", a)
var b [5]string = [5]string"ac", "ec", "bc", "fa", "cc"
sort.Strings(b[:])
fmt.Println("b:", b)
var c [5]float64 = [5]float6429.38, 22.32, 0.8, 88.8, 55.5
sort.Float64s(c[:])
fmt.Println("c:", c)
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
a: [0 1 2 3 4 5]
b: [ac bc cc ec fa]
c: [0.8 22.32 29.38 55.5 88.8]
PS E:\\TEXT\\test_go\\one>
倒序转换 intTmp 为 IntSlice 类型
package main
import (
"fmt"
"sort"
)
func main()
intTmp := []int
6, 3, 9, 8, 1, 2, 5, 7,
//转换intTmp为IntSlice类型
sort.Sort(sort.IntSlice(intTmp))
fmt.Println(intTmp)
//[1 2 3 5 6 7 8 9]
sort.Sort(sort.Reverse(sort.IntSlice(intTmp)))
fmt.Println(intTmp)
//[9 8 7 6 5 3 2 1]
结构体切片排序
package main
import (
"fmt"
"sort"
)
type Person struct
Name string
Age int
func TestSortSlice()
people := []Person
"Bob", 31,
"John", 42,
"Michael", 17,
"Jenny", 26,
// 稳定排序正序
sort.SliceStable(people, func(i, j int) bool
return people[i].Age < people[j].Age
)
fmt.Println(people)
// 稳定排序倒序
sort.SliceStable(people, func(i, j int) bool
return people[i].Age > people[j].Age
)
fmt.Println(people)
func main()
TestSortSlice()
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
[Michael 17 Jenny 26 Bob 31 John 42]
[John 42 Bob 31 Jenny 26 Michael 17]
PS E:\\TEXT\\test_go\\one>
map 按切片按值排序
package main
import (
"fmt"
"sort"
)
func main()
//待排序队列
var stuScore = map[string]int"ee": 20, "cc": 90, "ff": 70, "qq": 40, "aa": 79, "bb": 30
//创建切片,遍历map的值
var nums = []int
for _, v := range stuScore
nums = append(nums, v)
//对切片按值排序
//sort.Ints(nums)
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
fmt.Println(nums)
for _, num := range nums
//遍历map的key和值,与切片中的元素做比对
for k, v := range stuScore
if v == num
fmt.Println(k, v)
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
[90 79 70 40 30 20]
cc 90
aa 79
ff 70
qq 40
bb 30
ee 20
PS E:\\TEXT\\test_go\\one>
go 输出 26 个英文字母
func test1()
for i := 'A'; i <= 'Z'; i++
fmt.Printf("%c", i)
指针传参
package main
import "fmt"
func change(val *int)
*val = 55
func main()
a := 58
fmt.Println("value of a before function call is", a)
b := &a
change(b)
fmt.Println("value of a after function call is", a)
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
value of a before function call is 58
value of a after function call is 55
PS E:\\TEXT\\test_go\\one>
make & new 的区别以及值拷贝和引用拷贝
make 用来分配引用类型的内存,比如 map、slice 以及 channel 。
new 用来分配除引用类型的所有其他类型的内存,比如 int 、数组等。
package main
import "fmt"
func main()
var a *int = new(int)
*a = 100
fmt.Printf("*a=%d\\n", *a)
var b *[]int = new([]int)
fmt.Printf("*b=%v\\n", *b)
(*b) = make([]int, 5, 100)
(*b)[0] = 100
(*b)[1] = 200
fmt.Printf("*b=%v\\n", *b)
PS E:\\TEXT\\test_go\\one> go run .\\test1.go
*a=100
*b=[]
*b=[100 200 0 0 0]
PS E:\\TEXT\\test_go\\one>
1 写一个程序,获取一个变量的地址,并打印到终端
package main
import "fmt"
func main()
var a int = 100
fmt.Printf("addr:%p\\n", &a)
// addr:0xc00000e0b8
2 写一个函数,传入一个 int 类型的指针,并在函数中修改所指向的值
package main
import "fmt"
func modify(a *int以上是关于Golang M 2023 4 topic的主要内容,如果未能解决你的问题,请参考以下文章
Golang basic_leamingM 2023 3 topic