Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
Fun with Go interfaces. In this case using io.Writer
package main
import (
type RedisFile struct{
connection redis.Conn // should this be a pointer?
name string
// establish a new redis connection and use name as the working key for the file
func (rf RedisFile) Open(name string) RedisWriter {
var err error
rf.connection, err = redis.DialTimeout("tcp", "" , 0, 1*time.Second, 1*time.Second)
if err != nil{
} = name
return RedisWriter {rf}
type RedisWriter struct{
file RedisFile
func (rw RedisWriter) Write(p []byte) (n int, err error){
_, rerr := rw.file.connection.Do("APPEND",, p)
if rerr != nil {
rw.file.connection.Do("PUBLISH", "cp_test_chan", // publish the file into the file name channel
return len(p), nil
func main(){
f := RedisFile{}
r, err := os.Open(os.Args[1])
w := f.Open(filepath.Base(r.Name()))
if err != nil {
defer r.Close()
_, err = io.Copy(w, r) // Since our RedisWriter satisfies the Writer Interface we can just use io.Copy like it aint no thang.
if err != nil {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment