Skip to content

Instantly share code, notes, and snippets.

@RameshRM
Created December 29, 2017 23:00
Show Gist options
  • Save RameshRM/3de173885ddb7010add21fbac74c5535 to your computer and use it in GitHub Desktop.
Save RameshRM/3de173885ddb7010add21fbac74c5535 to your computer and use it in GitHub Desktop.
// _Channels_ are the pipes that connect concurrent
// goroutines. You can send values into channels from one
// goroutine and receive those values into another
// goroutine.
package main
import "bufio"
import "fmt"
import "net"
import "strings"
import "os"
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
// Create a new channel with `make(chan val-type)`.
// Channels are typed by the values they convey.
// messages := make(chan string)
//
// // _Send_ a value into a channel using the `channel <-`
// // syntax. Here we send `"ping"` to the `messages`
// // channel we made above, from a new goroutine.
// go func() {
// addr, err := net.LookupHost("google.com")
// if(err == nil) {
// fmt.Println("NO ERRROR")
// }
// messages <- strings.Join(addr, "||")
// }()
// The `<-channel` syntax _receives_ a value from the
// channel. Here we'll receive the `"ping"` message
// we sent above and print it out.
// msg := <-messages
// fmt.Println(msg)
f, err := os.Create("result.csv")
check(err)
defer f.Close()
w := bufio.NewWriter(f)
lookupFuture := doLookup("google.com")
fmt.Println("result:",lookupFuture)
w.WriteString("lookupFuture")
// w.Flush()
}
//
// func futureData(inputAddr string) <-chan string {
// c := make(chan data, 1)
//
// go func() {
// var body []byte
// var err error
//
// resp, err := http.Get(url)
// defer resp.Body.Close()
//
// body, err = ioutil.ReadAll(resp.Body)
//
// c <- data{Body: body, Error: err}
// }()
//
// return c
// }
func doLookup(inputAddr string) <-chan string {
lookupFn := make(chan string)
go func() {
addr, err := net.LookupAddr(inputAddr)
if(err == nil) {
fmt.Println("NO ERRROR")
fmt.Println(addr)
lookupFn <- strings.Join(addr , ",")
} else{
lookupFn <- "Err"
}
close(lookupFn)
}()
return lookupFn
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment