亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將值發送到另一個函數 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 關注
  • 249 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號