我如何动态遍历一个包? [英] How do I dynamically iterate through a package?

查看:123
本文介绍了我如何动态遍历一个包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Go的第一周,所以请原谅无知;)。我从Python中安然无恙。我正在构建一个简单的带有加法和减法的计算器。


$ b 我的 addition.go 文件如下所示:

 包计算器

类型添加struct {}

func(h Add)首先(x int)int {
x + = 5
return x
}

func(h Add)Second(x int)int {
x + = 10
return x
}

我的 subtraction.go 文件看起来非常类似于 addition.go 和未来的功能,如 multiplication.go division .go 看起来很相似。要运行这些我有:

  package main 

import(
fmt
github.com/mytester/calculator


类型Calc接口{
First(x int)int
Second(x int)int

$ b $ func main(){
x:= 10
var i Calc
a:=& calculator.Add {}
i = a
i.First(x)
fmt.Println(x)
fmt.Println(i.First(x))
fmt.Println(i.Second(x))

fmt.Println(Next method ...)
b:=& calculator.Sub {}
i = b
fmt.Println(x)
fmt.Println(i.First(x))
fmt.Println(i.Second(x))

//遍历其余的计算器方法

}

这看起来非常冗长,尤其是当我添加了越来越多的功能如乘法等等。有没有办法找到我的计算器的所有方法&然后遍历所有这些?我查看了 reflect 文档,但似乎没有办法得到它。它们的运行顺序无关紧要。例如,减法可以在加法运行之前运行。

解决方案

Go没有提供你想要的功能。没有机制来反思包的内容。原因是编译器只保留可执行文件中引用的函数和变量。如果你从不显式使用函数,它将不在可执行文件中。对一组可能不完整的符号进行迭代是没有意义的,因此没有实现。



您可以创建一个包含您想要操作的类型的对象的数组,并迭代如果这可以解决您的问题。


This is my first week with Go, so please forgive the ignorance ;). I come in peace from Python. I am building a simple calculator with Addition and Subtraction currently.

My addition.go file looks like this:

package calculator

type Add struct{}

func (h Add) First(x int) int {
    x += 5
    return x
}

func (h Add) Second(x int) int {
    x += 10
    return x
}

The layout of my subtraction.go file looks very similar to addition.go and future features like multiplication.go and division.go will look similar. To run these I have:

package main

import (
    "fmt"
    "github.com/mytester/calculator"
)

type Calc interface {
    First(x int) int
    Second(x int) int
}

func main() {
    x := 10
    var i Calc
    a := &calculator.Add{}
    i = a
    i.First(x)
    fmt.Println(x)
    fmt.Println(i.First(x))
    fmt.Println(i.Second(x))

    fmt.Println("Next method...")
    b := &calculator.Sub{}
    i = b
    fmt.Println(x)
    fmt.Println(i.First(x))
    fmt.Println(i.Second(x))

    // iterate through the rest of my calculator methods

}

This seems very verbose, especially as I add more and more features like multiplication, etc. Is there a way to find all the methods of my calculator & then iterate over all of them? I've looked through the reflect documentation but there doesn't seem to be a way to get this. The order in which they are run doesn't matter....eg subtraction can run before addition.

解决方案

Go does not provide the feature you want. There is no mechanism to introspect the contents of packages. The reason for this is that the compiler keeps only functions and variable in the executable that are referenced somewhere. If you never explicitly use a function, it won't be in the executable. Iterating over a possibly incomplete set of symbols is pointless and is therefore not implemented.

You could make an array containing objects of the types you want to operate on and iterate on that if that solves your problem.

这篇关于我如何动态遍历一个包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆