为了账号安全,请及时绑定邮箱和手机立即绑定

将值发送到另一个函数 golang

将值发送到另一个函数 golang

Go
慕妹3146593 2022-03-03 16:08:35
我做了一个有 4 个房间的随机房间程序。我正在尝试获取每个房间的属性并将它们传递给其他功能。每个房间里都有一个人,有一个名字和一个年龄属性。我正在尝试传递这些属性来测试 if 语句以发出额外的响应。如何传递这些值?//the random maze room gamepackage main//All imports can be combined into ()import ("fmt"        //Import needed for random operation        "math/rand"        //Import requied to call upon current time         "time")type Person struct {    Name string    Age int}func main(){    // These two lines are designed to reset the random Seed every time the program is run     // Unless the randomizer is seeded, Golang 1.6 is pseudo-random,     // always defaulting to Seed(1)     s1 := rand.NewSource(time.Now().UnixNano())    r1 := rand.New(s1)    switch r1.Intn(4){    case 0:        westRoom()    case 1:        eastRoom()    case 2:        northRoom()    case 3:        southRoom()    default:        lostRoom()    }    p := Person{}    p.Name = Avatarname    p.Age = Avatarage    avatar(&p)    appearance(&p)  }func westRoom(){    fmt.Println("You find yourself in a room with three walls, and a door behind you.")    fmt.Println("The opposite wall is a window, overlooking the sea")    Avatarname := "Bill"    Avatarage := 25    return}func eastRoom(){    fmt.Println("You find yourself in a room with a door on the walls to your left, right, and behind you")    fmt.Println("on the wall across from you is a painting of a mountain scene")    Avatarname := "Mary"    Avatarage := 33    return}func northRoom(){    fmt.Println("You find yourself in a room with a door on the wall behind you")    fmt.Println("You see several statues of people standing around the room")    Avatarname := "Joe"    Avatarage := 58    return}func southRoom(){    fmt.Println("You find yourself in a room with a door on the wall in front and behind you")    Avatarname := "Abagail"    Avatarage := 67    return}func lostRoom(){    fmt.Println("You are unable to find a room in a maze filled only with rooms")    fmt.Println("It's almost like the programmer didn't know what he was doing")}
查看完整描述

2 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

一种方法是更改房间函数以返回Person结构或结构的属性Person。


在此处查看实时代码:The Go Playground


例如,


func northRoom() *Person{

  fmt.Println("You find yourself in a room with a door on the wall behind you")

  fmt.Println("You see several statues of people standing around the room")

  return &Person{"Joe", 58}

}

switch现在,您的语句中的函数调用可以期望Person返回一个结构。


var p *Person

switch r1.Intn(4){

case 0:

    p = westRoom()

case 1:

    p = eastRoom()

case 2:

    p = northRoom()

case 3:

    p = southRoom()

default:

    p = lostRoom()

}


avatar(p)

appearance(p) 

请注意,您需要更改avatar()和appearance()函数的签名以接受*Person参数。


查看完整回答
反对 回复 2022-03-03
?
弑天下

TA贡献1818条经验 获得超8个赞

为什么不让您的房间功能返回 a Person?例子:


func main() {

    // These two lines are designed to reset the random Seed every time the program is run

    // Unless the randomizer is seeded, Golang 1.6 is pseudo-random,

    // always defaulting to Seed(1)

    s1 := rand.NewSource(time.Now().UnixNano())

    r1 := rand.New(s1)


    var p Person

    switch r1.Intn(4) {

    case 0:

        p = westRoom()

    case 1:

        p = eastRoom()

    case 2:

        p = northRoom()

    case 3:

        p = southRoom()

    default:

        lostRoom()

    }


    p.printAvatar()

    p.printAppeareance()


}


func westRoom() Person {

    fmt.Println("You find yourself in a room with three walls, and a door behind you.")

    fmt.Println("The opposite wall is a window, overlooking the sea")


    return Person{

        Name: "Bill",

        Age:  25,

    }

}

https://play.golang.org/p/9_PuPsRdXg


查看完整回答
反对 回复 2022-03-03
  • 2 回答
  • 0 关注
  • 263 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号