-
-
Save whyrusleeping/7060813 to your computer and use it in GitHub Desktop.
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 ( | |
"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) | |
} | |
} |
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 ( | |
"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) | |
} |
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
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