Last active
October 8, 2018 14:15
-
-
Save unanoc/df7b256597e2e4af0a56ff37a9c20839 to your computer and use it in GitHub Desktop.
К примеру, нужно написать панель мониторинга, на которой выводите какие-то данные, и данные эти из одного источника приходят в виде float64 значений, а из другого в виде строк ("failed", "success" и т.п.). Как вы реализуете функцию, которая получает значения по каналу и выводит их на экран?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Renderer interface { | |
Render() | |
} | |
type ( | |
MyInt int | |
MyString string | |
) | |
func (i MyInt) Render() { | |
fmt.Println(i) | |
} | |
func (s MyString) Render() { | |
fmt.Println(s) | |
} | |
func Display(inputValues chan Renderer) { | |
for value := range inputValues { | |
value.Render() | |
} | |
} | |
func main() { | |
ch := make(chan Renderer) | |
go Display(ch) | |
ch <- MyInt(1) | |
ch <- MyString("hello") | |
} | |
// Неверное решение | |
// func Display(ch chan interface{}) { | |
// for v := range ch { | |
// switch x := v.(type) { | |
// case float64: | |
// RenderFloat64(x) | |
// case string: | |
// RenderString(x) | |
// } | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment