func WriteAsWindows1252 (src string, dst string) error {
bSrc, err := ioutil.ReadFile(src)
if err != nil {
return err
}
bDst := make([]byte, len(bSrc)*2)
replaceNonAscii := runes.Map(func(r rune) rune {
if r > unicode.MaxASCII {
return rune('?')
}
return r
})
transformer := transform.Chain(replaceNonAscii, charmap.Windows1252.NewEncoder())
_, _, err = transformer.Transform(bDst, bSrc, true)
if err != nil {
return err
}
return ioutil.WriteFile(dst, bDst, 0644)
}
runes.Map(func(r rune) rune {})
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
> > This does not work in cases where someone want to use a Transformer in a streaming context (e.g. to create a Reader or Writer or to include the Transform in a Chain).This really is what i was looking to implement, somehow,
src -> transform -> transform -> sink
> I've used bufio.Scanner to implement a custom transforming stream reader.
Indeed, that is a step forward for a much better implementation than previous solution. thanks!
Is there any formalized stream transform like apis in go that i missed ? Something like another language implements :x
Le mardi 28 juin 2016 11:54:25 UTC+2, Tamás Gulácsi a écrit :I've used bufio.Scanner to implement a custom transforming stream reader.
--
fs.createReadStream("some.file")
.on('error', console.error.bind(console))
.pipe(split(/\r?\n/))
.pipe(through(function (byteData, enc, next) {
next(null, byteData.toString()
.replace(/[^\x00-\x7F]/g, "?"))
}))
.pipe(through(function (byteData, enc, next) {
next(null, byteData.toString()
+ "\r\n")
}))
.pipe(iconv.encodeStream('win1252'))
.on('error', console.error.bind(console))
.pipe(fs.createWriteStream('file-in-win1252.txt'))
.on('error', console.error.bind(console))
package main
import (
"os"
"io"
"compress/gzip"
)
func main () {
pr, pw := io.Pipe()
go func () {
decoder, _ := gzip.NewReader(pr)
io.Copy(os.Stdout, decoder)
}()
archiver := gzip.NewWriter(pw)
defer archiver.Close()
io.Copy(archiver, os.Stdin)
}
package main
import (
"fmt"
"os"
// "bytes"
// "encoding/base64"
"github.com/mh-cbon/stream/stream3"
)
func main () {
// src, _ := os.Open("main.go")
stdin := os.Stdin
s := stream.CreateStreamReader(stdin)
// s.Pipe(stream.B64Encode(base64.StdEncoding)).Pipe(stream.CreateStreamWriter(os.Stdout))
s.Pipe(stream.GzipEncode()).Pipe(stream.GzipDecode()).Pipe(stream.CreateStreamWriter(os.Stdout))
err := s.Consume()
if err!=nil {
fmt.Println(err)
}
stdin.Close()
}
I honestly admit i have no real understanding of what i m doing ...
I can only figure out it works on the other end :o
But its way more readable, to me, src -> transform -> transform -> sink
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/rSHo0N7yDeQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/rSHo0N7yDeQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.