gpt4 book ai didi

Go:基本的 for 循环和 strconv

转载 作者:数据小太阳 更新时间:2023-10-29 03:47:20 27 4
gpt4 key购买 nike

<分区>

我是go语言的大一新生,想请教一些基础的东西,这个函数怎么理解?
我们需要使用“strconv”来解决这个问题。

  package main

import (
"fat"
"strconv"

)
type Student struct {
Name string
}
func (stu *Student) Leave() {
fmt.Println(stu.Name + " Leaving")

}

func (stu *Student) Present() {
fmt.Println("I am " + stu.Name)

}

func main() {
fmt.Println("Start of session")
for i := 0; i < 6; i++ {
s := Student{Name: fmt.Sprintf("Student%d", i)}
s.Present()
fmt.Println("Room Empty")
defer s.Leave()
}
fmt.Println("End of session")
}

输出应该是这样的

Start of session 
I am Student0
I am Student1
I am Student2
I am Student3
I am Student4
I am Student5
End of session
Student5 Leaving
Student4 Leaving
Student3 Leaving
Student2 Leaving
Student1 Leaving
Student0 Leaving
Room Empty

我们只需要编写一个 main function() 和一个简单的 for 循环就可以得到结果。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com