function - Go语言中如何调用另一个函数内的函数?

标签 function go struct boolean

我收到一条错误消息,提示“未定义有效”。如何在一个函数中调用另一个函数?

package main

import "fmt"

type Position struct {
    row int
    col int
}

func (posstn Position) isvalid() bool {
    if posstn.row > 8 || posstn.row < 0 || posstn.col > 8 || posstn.col < 0 {
        return false
    }
    return true
}

func Possmov(pos Position) {
    var isval isvalid
    if isval == true {
        fmt.Println("Something")
    }
}

func main() {
    Possmov(Position{1, 7})
}

最佳答案

您可以像这样调用 isvalid() pos.isvalid() 请参阅此工作示例代码:

package main

import "fmt"

type Position struct {
    row int
    col int
}

func (posstn Position) isvalid() bool {
    if posstn.row > 8 || posstn.row < 0 || posstn.col > 8 || posstn.col < 0 {
        return false
    }
    return true
}

func Possmov(pos Position) {
    isval := pos.isvalid()
    if isval == true {
        fmt.Println("Something")
    }
}
func main() {
    Possmov(Position{1, 7}) //Something
}

关于function - Go语言中如何调用另一个函数内的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38736732/

相关文章:

dictionary - 在 golang 中格式化 map[]

database - 如何解决 go 编程语言中的数据库结构问题?

pointers - 指向结构满足接口(interface)的指针

Javascript - deepEqual 比较和递归

c - 限制结构内的关键字和指针

mongodb - 服务器在 SASL 身份验证步骤 : Authentication failed 上返回错误

go - 如何获取 Go 中 map 中的条目数量?

c++ - 编写同时适用于 C 和 C++ 的结构代码

c++ - 索引哈希表

JavaScript:函数在循环时给出 "is not a function"