Create a gist now

Instantly share code, notes, and snippets.

@tebeka /md5.go
Created Nov 21, 2012

What would you like to do?
package main
import (
// open open a file, the filename is - will return os.Stdin
func open(filename string) (io.ReadCloser, error) {
if filename == "-" {
return os.Stdin, nil
file, err := os.Open(filename)
if err != nil {
return nil, err
return file, err
func main() {
var filename string
flag.StringVar(&filename, "file", "-", "file name")
file, err := open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "error: can't open %s - %s\n", filename, err)
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
fmt.Fprintf(os.Stderr, "error: can't read %s - %s\n", filename, err)
fmt.Printf("%x\n", hash.Sum(nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment