使用multiWriter将写入器添加到io.Writer中。

huangapple go评论85阅读模式
英文:

go: Add writers to io.writer using multiWriter

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习Go语言。我有一个指示哪些写入器处于活动状态的映射:

var writers map[int]bool

我想遍历这个映射,并使用multiWriter()创建一个表示所有活动写入器的写入器。

我正在使用以下代码片段:

func setupMultiLoggers() {
    var mw io.Writer
    for k, v := range writers {
        if v != true {
            continue
        }
        switch k {
        case 0:
            if mw == nil {
                mw = writer0
            } else {
                mw = io.MultiWriter(mw, writer0)
            }
        case 1:
            if mw == nil {
                mw = os.Stdout
            } else {
                mw = io.MultiWriter(mw, os.Stdout)
            }
        case 2:
            if mw == nil {
                mw = writer2
            } else {
                io.MultiWriter(mw, writer2)
            }
        default:
        }
    }
    log.SetOutput(mw)
}

当将映射的所有3个值初始化为true,然后测试writer2时,有时它能正常工作,有时不能(case 2中的代码总是执行)。

如果我只使用以下代码:

log.SetOutput(io.MultiWriter(writer0, os.Stdout, writer2))

它总是按预期工作。

我无法理解为什么我的原始代码不能可靠地工作。我有一种感觉,有一种更简洁的方法来“连接”写入器。

编辑:我找到了(愚蠢的)错误。第三个case中缺少对mw的赋值。
仍然在寻找一种更简洁的方法来“连接”写入器。

英文:

I'm new to Go.
I have a map which indicates which writers are active:

var writers map[int]bool

I want to iterate over the map, and create a writer which represents all active writers using multiWriter()

I am using the following code snippet:

func setupMultiLoggers() {
var mw io.Writer
for k, v := range writers {
	if v != true {
		continue
	}
	switch k {
	case 0:
		if mw == nil {
			mw = writer0
		} else {
			mw = io.MultiWriter(mw, writer0)
		}
	case 1:
		if mw == nil {
			mw = os.Stdout
		} else {
			mw = io.MultiWriter(mw, os.Stdout)
		}
	case 2:
		if mw == nil {
			mw = writer2
		} else {
			io.MultiWriter(mw, writer2)
		}
	default:
	}
}
log.SetOutput(mw)

}

When initializing the all 3 values of the map to true and then testing writer2, it sometimes works and sometimes doesn't (code in case 2 always executes)

If I just use

log.SetOutput(io.MultiWriter(writer0, os.Stdout, writer2))

It always works as expected.

I cannot seem to understand why my original code doesn't reliably work. I have a feeling there is a more clean way to "concatenate" writers

EDIT: I've found my (stupid) bug. assignment to mw was missing in the third case.
Still looking for a more clean way to "concatenate" writers.

答案1

得分: 3

你似乎没有一个作家的映射,而是一个整数到布尔值的映射。可以假设这些整数代表文件描述符。

最简单的方法可能是将其从map[int]bool更改为map[io.Writer]bool,然后可以遍历它们并将它们添加到一个切片中。

ws := make([]io.Writer, 0)
for k, v := range writers {
    if v != true {
        continue
    }
    ws = append(ws, k)
}

之后,你可以直接通过将切片传递给MultiWriter函数来创建一个多写入器。

w := io.MultiWriter(ws...)
log.SetOutput(w)

(在切片后的函数参数中的...表示将切片展开为函数调用的参数)

如果要保持map[int]bool的方式,也可以做同样的事情,但是你需要类似于你已经有的switch语句来将整数转换为io.Writer。

英文:

You don't seem to have a map of writers, you have a map of ints to bools. Presumably, the ints represent the file descriptor.

The easiest way to do it would probably to change it from map[int]bool to map[io.Writer]bool, then you can iterate through them and add them to a slice with append.

ws := make([]io.Writer, 0)
for k, v : = range(writers) {
    if v != true {
        continue
    }
    ws = append(ws, k)
}

After that, you can just create a multiwriter directly by giving the slice to your MultiWriter call.

w := io.MultiWriter(ws...)
log.SetOutput(w)

(The ... in a function parameter after a slice means to expand the slice into arguments to the function call)

You can do the same thing while keeping it map[int]bool, but you'll need a switch similar to what you already have to convert your int into an io.Writer

答案2

得分: 1

例如,

var writers map[int]bool

var writer0, writer2 io.Writer

func setupMultiLoggers() {
    var mw []io.Writer
    for k, v := range writers {
        if v == false {
            continue
        }
        switch k {
        case 0:
            mw = append(mw, writer0)
        case 1:
            mw = append(mw, os.Stdout)
        case 2:
            mw = append(mw, writer2)
        }
    }
    log.SetOutput(io.MultiWriter(mw...))
}
英文:

For example,

var writers map[int]bool

var writer0, writer2 io.Writer

func setupMultiLoggers() {
    var mw []io.Writer
    for k, v := range writers {
        if v == false {
            continue
        }
        switch k {
        case 0:
            mw = append(mw, writer0)
        case 1:
            mw = append(mw, os.Stdout)
        case 2:
            mw = append(mw, writer2)
        }
    }
    log.SetOutput(io.MultiWriter(mw...))
}

huangapple
  • 本文由 发表于 2016年2月8日 05:48:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/35259530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定