Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Last active December 25, 2015 23:59
Show Gist options
  • Save whyrusleeping/7060813 to your computer and use it in GitHub Desktop.
Save whyrusleeping/7060813 to your computer and use it in GitHub Desktop.
package main
import (
"net"
"bufio"
)
//Handles a connection
func handleConnection(conn net.Conn) {
//There are a few ways to read from the connection
//My favorite is with a bufio Reader
read := bufio.NewReader(conn)
//You can read a single byte
b, err := read.ReadByte()
//You can read a deliminated string
str, err := read.ReadString('\n')
//You can read a set amount of bytes
buf := make([]byte, 256)
nread, err := read.Read(buf) //Read 256 bytes
//And with any of these 'two return' functions, you can leave one arg out
str, _ := read.ReadString('\n')
}
func main() {
//Listen for any tcp connection to port 8040
list, err := net.Listen("tcp", ":8040")
//Loop FOREVER AND EVER
for {
conn, err := list.Accept()
if err != nil {
//Handle error, print a message or something
}
//Asynchronously handle the connection and continue listening
go handleConnection(conn)
}
}
package main
import (
"crypto/md5"
"fmt"
)
func main() {
bytestohash := []byte("thisisa string reenting a sample file content")
h := md5.New()
h.Write(bytestohash)
hashitself := h.Sum(nil)
fmt.Println(hashitself)
}
var myslice []string
toremove := 4
myslice = append(myslice[:4], mslice[4 + 1:]...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment