func main() { var straddr string flag.StringVar(&straddr, "a", "", "Address of the exporter") flag.Parse() if straddr == "" { flag.Usage() log.Exit("No address given") } bstdin := bufio.NewReader(os.Stdin) var err os.Error done := make(chan os.Error) imp, err := netchan.NewImporter("tcp", straddr) if err != nil { log.Exit("Error creating importer: ", err) } go func() { for line, err := bstdin.ReadString('\n'); err == nil; line, err = bstdin.ReadString('\n') { go importChan(imp, strings.TrimSpace(line), done) go func() { err := <-done log.Println(err) }() } log.Exit("Error reading stdin: ", err) }() <-done }
func main() { addr, err := net.ResolveTCPAddr("127.0.0.1:4009") if err != nil { log.Exit("error:", err) } listener, err := net.ListenTCP("tcp", addr) if err != nil { log.Exit("error", err) } //1 channel for incoming connections, another for client communication connections := make(chan *net.TCPConn) clients := make(chan TClient) cMap := make(map[string]*net.TCPConn) fMap := make(map[string]string) go ListenConnections(listener, connections, clients) log.Stdout("Waiting for connections\n") for { select { case conn := <-connections: cMap[conn.RemoteAddr().String()] = conn case client := <-clients: if regexp.MustCompile("^have ").MatchString(client.msg) { fMap[string(client.msg[5:len(client.msg)])] = client.local } if regexp.MustCompile("^list").MatchString(client.msg) { for key, value := range fMap { cMap[client.forserver].Write(strings.Bytes(key + "->" + value)) } cMap[client.forserver].Write(strings.Bytes("\n")) } } } }
func audioServer() { // Native Client requires that all audio calls // original from a single OS thread. runtime.LockOSThread() n, err := av.AudioStream(nil) if err != nil { log.Exit(err) } for { b := <-sndc for len(b)*2 >= n { var a []uint16 a, b = b[0:n/2], b[n/2:] n, err = av.AudioStream(a) if err != nil { log.Exit(err) } println(n, len(b)*2) } a := make([]uint16, n/2) for i := range b { a[i] = b[i] } n, err = av.AudioStream(a) } }
func newIRC(c net.Conn) (irc *IRC) { irc = &IRC{Reader: make(chan string, 10), Writer: make(chan string, 10)} // start reader go func(r *bufio.Reader) { for { line, err := r.ReadString('\n') if err != nil { log.Exit("Unable to read from channel", err) } log.Stdoutf("Read: %#v", line) irc.Reader <- line } }(bufio.NewReader(c)) // start writer go func(w *bufio.Writer) { for { line := <-irc.Writer _, err := w.WriteString(line) if err != nil { log.Exit("Unable to write to channel", err) } err = w.Flush() if err != nil { log.Exit("Unable to write to channel", err) } log.Stdoutf("Wrote: %#v", line) } }(bufio.NewWriter(c)) return irc }
func main() { // Read the string inputReader := bufio.NewReader(os.Stdin) var info = Metadata{"", uint32(0), nil} var cmd = Command{"", nil, nil, nil, ""} infoJson, err := inputReader.ReadString('\n') if err != nil { log.Exit(err) } else { json.Unmarshal(infoJson, &info) } bpt, bperr := bptree.NewBpTree(info.Filename, info.Keysize, info.Fieldsizes) if !bperr { log.Exit("Failed B+ tree creation") } else { fmt.Println("ok") } alive := true for alive { cmdJson, err := inputReader.ReadString('\n') if err != nil { alive = false break } if cmdJson == "q\n" { alive = false } else { json.Unmarshal(cmdJson, &cmd) if cmd.Op == "insert" { result := bpt.Insert(cmd.LeftKey, cmd.Fields) fmt.Println(result) } else if cmd.Op == "find" { records, ack := bpt.Find(cmd.LeftKey, cmd.RightKey) for record := range records { json.Marshal(os.Stdout, map[string]interface{}{ "key": record.GetKey(), "value": record.AllFields()}) fmt.Println() ack <- true } fmt.Println("end") } else if cmd.Op == "visualize" { bptree.Dotty(cmd.FileName, bpt) } else if cmd.Op == "prettyprint" { s := fmt.Sprintln(bpt) f, _ := os.Open(cmd.FileName, os.O_RDWR|os.O_CREAT, 0666) f.Write([]byte(s)) f.Close() } } } fmt.Println("exited") }
// Allocates a new key block. This isn't quite as convient as the method // for BTrees as we have to tell it if we are allocating an internal or an // external block. func (self *BpTree) allocate(dim *BlockDimensions) *KeyBlock { if dim != self.external && dim != self.internal { log.Exit("Cannot allocate a block that has dimensions that are niether the dimensions of internal or external nodes.") } block, ok := NewKeyBlock(self.bf, dim) if !ok { log.Exit("Could not allocate block PANIC") } return block }
func main() { if len(os.Args) != 2 { log.Exit("Usage: " + os.Args[0] + " n") } i, err := strconv.Atoi(os.Args[1]) if err != nil { log.Exit(err) } res := fib(i) println(res) }
func InitTables(dbh *sqlite3.Handle) { st, err := dbh.Prepare("CREATE TABLE IF NOT EXISTS entry (id INTEGER PRIMARY KEY, body VARCHAR(255));") if err != "" { log.Exit(err) } if st.Step() != sqlite3.SQLITE_DONE { log.Exit(dbh.ErrMsg()) } if st.Finalize() != sqlite3.SQLITE_OK { log.Exit(dbh.ErrMsg()) } }
func main() { // Dial freenode log.Stdout("Dialing server") server, err := IRCDial("tcp", "", "irc.freenode.net:6667") // TODO: make this not hardcoded if err != nil { log.Exit("Dialing error:", err) } // Login to the server log.Stdout("Logging in to server") server.Login("goo_bot", FlagInvisible) // Send a PM to NickServ to identify log.Stdout("Identifying to Nickserv") server.PrivMsg("NickServ", "identify go_bot turing") // Join a chat log.Stdout("Joining #bottest") bottest, _ := server.Join("#bottest") // TODO: log the errors // Send the chat a message log.Stdout("Greeting #bottest") bottest.Write("hi guys!") // We're done with the connection, close it log.Stdout("Sleeping before closing") time.Sleep(1e10) log.Stdout("Closing") server.Close() // Talk to server (loop forever) connReader := bufio.NewReader(server.Conn) for i := 0; i < 100; i++ { response, err := connReader.ReadString('\n') if err != nil { log.Exit("Error reading from connection:", err) } log.Stdoutf("RECEIVED: %s", strings.TrimSpace(response)) if response[0] != ':' { //not a private message wd := strings.Split(response, " ", 2) log.Stdout("Got Message ", wd[0]) switch wd[0] { // Message Type case "PING": // TODO: find a better way to remove leading character in string pongServer := string(strings.Bytes(wd[1])[1:len(wd[1])]) pong := "PONG " + pongServer + "\r\n" log.Stdout("SENT: ", pong) server.Conn.Write(strings.Bytes(pong)) } } } // We're done with the connection, close it log.Stdout("Sleeping before closing") time.Sleep(1e10) log.Stdout("Closing") server.Close() }
func main() { t := template.New(nil) t.SetDelims("«", "»") err := t.Parse(templateStr) if err != nil { log.Exit(err) } err = t.Execute(data, os.Stdout) if err != nil { log.Exit(err) } }
func main() { client, err := rpc.DialHTTP("tcp", "localhost:1234") if err != nil { log.Exit("dialing:", err) } args := &arith.Args{7, 8} reply := new(arith.Reply) err = client.Call("Arith.Add", args, reply) if err != nil { log.Exit("arith error:", err) } fmt.Printf("Arith: %d+%d=%d\n", args.A, args.B, reply.C) }
func main() { r, err := jsonrpc.Dial("tcp", "localhost:6666") if err != nil { log.Exit("Error dialing host: ", err) } remote := &TestRPCSrv{r} a := "foo" var b string err = remote.Call("testApp.PrintStuff", &a, &b) if err != nil { log.Exit("Error calling function: ", err) } log.Exit(b) }
func client(host string, port int) { client, err := rpc.DialHTTP("tcp", host+":"+fmt.Sprintf("%d", port)) if err != nil { log.Exit("dialing:", err) } var reply string err = client.Call("ProcessStarter.Start", processMap["server"], &reply) if err != nil { log.Exit("error:", err) } // fmt.Printf("Arith: %d*%d=%d", args.A, args.B, *reply) }
func main() { // This works r, _, err := http.Get("https://www.google.com") if err != nil { log.Exit(err) } log.Println(r) // This doesn't r, _, err = http.Get("https://streaming.campfirenow.com") if err != nil { log.Exit(err) } log.Println(r) }
func git_from_net(url string) string { var args [3]string args[0] = "git" args[1] = "clone" args[2] = url var fds []*os.File = new([3]*os.File) fds[0] = os.Stdin fds[1] = os.Stdout fds[2] = os.Stderr _, str := path.Split(url) name := strings.Split(str, ".", -1)[0] var git_path string switch os.Getenv("GOOS") { case "darwin": git_path = "/usr/local/git/bin/git" break case "linux": git_path = "/opt/local/bin/git" break } /* Replace this with git's full path, or use a shell, and then call git in the args */ pid, err := os.ForkExec(git_path, &args, os.Envs, os.Getenv("GOROOT")+"/src/pkg/", fds) if err != nil { log.Exit(err) } os.Wait(pid, 0) return string(os.Getenv("GOROOT") + "/src/pkg/" + name) }
func main() { // start a connection with the server irc, err := DialIRC("", "irc.freenode.net:6667") if err != nil { log.Exit("Error connecting:", err) } irc.Log("Hello, world!") // Login w/ a default name and user irc.Login("go_bot") // Join a chat. Yay LUG! irc.Join("#bottest") // Get messages, handle them for { // loop forever line := <-irc.Rec words := strings.Split(line, " ", 3) irc.Log(ParseUser(words[0])) switch words[1] { // Message Type case "PRIVMSG": go irc.Hi(words[2]) continue } } time.Sleep(6e10) // Wait a minute irc.Log("Closing") irc.Close() }
func main() { http.HandleFunc("/", ProxyServer) err := http.ListenAndServe(":8123", nil) if err != nil { log.Exit("ListenAndServe: ", err.String()) } }
func main() { // Dial freenode log.Stdout("Dialing server") server, err := irc.IRCDial("tcp", "", "irc.freenode.net:6667") // TODO: make this not hardcoded if err != nil { log.Exit("Dialing error:", err) } // Login to the server log.Stdout("Logging in to server") server.Login("goo_bot", irc.FlagInvisible) // Send a PM to NickServ to identify log.Stdout("Identifying to Nickserv") server.PrivMsg("NickServ", "identify go_bot turing") // Join a chat log.Stdout("Joining #bottest") bottest, _ := server.Join("#bottest") // TODO: log the errors // Send the chat a message log.Stdout("Greeting #bottest") bottest.Write("hi guys!") // We're done with the connection, close it log.Stdout("Sleeping before closing") time.Sleep(1e10) log.Stdout("Closing") server.Close() }
func main() { flag.Parse() go hub() http.Handle("/broadcast", websocket.Handler(clientHandler)) if err := http.ListenAndServe(*addr, nil); err != nil { log.Exit("ListenAndServe:", err) } }
func processEvent(ch <-chan interface{}) { for { if closed(ch) { log.Exit("X display closed.") } ev, ok := <-ch if !ok { // no events, return return } switch ev.(type) { case draw.ErrEvent: log.Exit("X11 err: ", ev.(draw.ErrEvent).Err) } } }
func main() { flag.Parse() http.Handle("/", http.HandlerFunc(QR)) err := http.ListenAndServe(*addr, nil) if err != nil { log.Exit("ListenAndServe:", err) } }
func getstdin() string { bytes := make([]byte, 1000) n, err := os.Stdin.Read(bytes) if err != nil { log.Exit("Could not read from stdin", err) } return string(bytes[0:n]) }
func main() { calc, err := NewCalcServiceClient("MyCalcService", "tcp", "", "127.0.0.1:1234") if err != nil { log.Exit("cant setup calc service:", err) } doCalc(calc) }
//runs the web application and serves http requests func Run(addr string) { http.Handle("/", http.HandlerFunc(httpHandler)) log.Stdoutf("web.go serving %s", addr) err := http.ListenAndServe(addr, nil) if err != nil { log.Exit("ListenAndServe:", err) } }
// Accept accepts connections on the listener and serves requests // for each incoming connection. Accept blocks; the caller typically // invokes it in a go statement. func (server *Server) Accept(lis net.Listener) { for { conn, err := lis.Accept() if err != nil { log.Exit("rpc.Serve: accept:", err.String()) // TODO(r): exit? } go server.ServeConn(conn) } }
func (s *Server) Run(addr string) { mux := http.NewServeMux() mux.Handle("/", s) s.Logger.Printf("web.go serving %s\n", addr) err := http.ListenAndServe(addr, mux) if err != nil { log.Exit("ListenAndServe:", err) } }
func server() { ps := new(ProcessStarter) rpc.Register(ps) rpc.HandleHTTP() l, e := net.Listen("tcp", ":1234") if e != nil { log.Exit("listen error:", e) } go http.Serve(l, nil) }
func RunServer(addr string) { self := new(Server).Init() rpc.Register(self) rpc.HandleHTTP() l, e := net.Listen("tcp", addr) if e != nil { log.Exit("listen error: ", e) } http.Serve(l, nil) }
func main() { arith := new(arith.Arith) rpc.Register(arith) rpc.HandleHTTP() l, e := net.Listen("tcp", ":1234") if e != nil { log.Exit("listen error: ", e) } http.Serve(l, nil) }
func loadStartPosition(worldPath string) { file, err := os.Open(path.Join(worldPath, "level.dat"), os.O_RDONLY, 0) if err != nil { log.Exit("loadStartPosition: ", err.String()) } level, err := nbt.Read(file) file.Close() if err != nil { log.Exit("loadStartPosition: ", err.String()) } pos := level.Lookup("/Data/Player/Pos") StartPosition = XYZ{ pos.(*nbt.List).Value[0].(*nbt.Double).Value, pos.(*nbt.List).Value[1].(*nbt.Double).Value, pos.(*nbt.List).Value[2].(*nbt.Double).Value, } }