[go-nuts] How to seek in file use bufio.Reader?

2017-01-18 Thread hui zhang
I am using  bufio.Reader  and binary.Read 
to read binary from file,  I want to seek in the file or bufio.Reader
Below code does not work 

fi.Seek(0, os.SEEK_SET) Does not work for bufio.Reader


below definition shows that r control the position , but I did not see any 
method to set it

type Reader struct {
   buf  []byte
   rd   io.Reader // reader provided by the client
   r, w int   // buf read and write positions
   err  error
   lastByte int
   lastRuneSize int
}

I expect below code to output 3.13 twice ,   but it output 3.13   3.14

package main

import (
   _ "bytes"
   "fmt"
   _ "math"

   "bufio"
   "encoding/binary"
   "os"
)

func main() {
   var pi float64
   //b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
   // 0x78, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
   //buf := bytes.NewReader(b)
   //err := binary.Read(buf, binary.LittleEndian, )
   //if err != nil {
   // fmt.Println("binary.Read failed:", err)
   //}
   //fmt.Println(pi)
    Output: 3.141592653589793
   //err = binary.Read(buf, binary.LittleEndian, )
   //if err != nil {
   // fmt.Println("binary.Read failed:", err)
   //}
   //fmt.Println(pi)
   //
   // open output file
   fo, err := os.Create("./output.bin")
   if err != nil {
  panic(err)
   }

   // make a write buffer
   var fl1 float64 = 3.13
   var fl2 float64 = 3.14
   w := bufio.NewWriter(fo)
   err = binary.Write(w, binary.LittleEndian, fl1)
   if err != nil {
  fmt.Println("binary.Write failed:", err)
   }
   err = binary.Write(w, binary.LittleEndian, fl2)
   if err != nil {
  fmt.Println("binary.Write failed:", err)
   }
   w.Flush()
   // close fo on exit and check for its returned error
   if err := fo.Close(); err != nil {
  panic(err)
   }

   //
   fi, err := os.Open("./output.bin")
   if err != nil {
  panic(err)
   }
   // close fi on exit and check for its returned error
   defer func() {
  if err := fi.Close(); err != nil {
 panic(err)
  }
   }()

   rd := bufio.NewReader(fi)
   err = binary.Read(rd, binary.LittleEndian, )
   if err != nil {
  fmt.Println("binary.Read failed:", err)
   }
   fmt.Println(pi)

   fi.Seek(0, os.SEEK_SET) Does not work for bufio.Reader

   err = binary.Read(rd, binary.LittleEndian, )
   if err != nil {
  fmt.Println("binary.Read failed:", err)
   }
   fmt.Println(pi)

}

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] how to seek in file using go

2017-01-18 Thread Ayan George


On 01/19/2017 02:00 AM, hui zhang wrote:
> so the file seek does not work in bufio.Read  ?
> 

It looks like this has been answered before:

   https://groups.google.com/forum/#!topic/golang-nuts/mOvX0bmJoeI

You could also create another bufio.Reader after you seek like:

   rd := bufio.NewReader(fi)
   err = binary.Read(rd, binary.LittleEndian, )
   fmt.Println(pi)


   fi.Seek(0, os.SEEK_SET )?

   rd = bufio.NewReader(fi)
   err = binary.Read(rd, binary.LittleEndian, )
   fmt.Println(pi)

If you look at the bufio.Reader struct, it doesn't appear to have a
notion of where it is in the file -- it just contains a buffer, read and
write positions within that buffer, etc.

-ayan

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] how to seek in file using go

2017-01-18 Thread Konstantin Khomoutov
On Wed, 18 Jan 2017 23:00:33 -0800 (PST)
hui zhang  wrote:

> so the file seek does not work in bufio.Read  ?

You might need to use the Reset() method of your bufio.Reader value
before you reposition the pointer in the underlying opened file.

Please see `go doc bufio.Reader.Reset`.

But this poses the question of whether you really need buffered I/O.
May be just use io.ReadFull() helper to read data in whole chunks from
the file directly.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] how to seek in file using go

2017-01-18 Thread Konstantin Khomoutov
On Wed, 18 Jan 2017 22:54:31 -0800 (PST)
hui zhang  wrote:

[...]
> > 2. So we now read the docs on os.File: 
> >
> >  $ go doc os.File 
> >
> >...and see all of its methods there, inclusing Seek(). 
> >
> > 3. So we read the docs on it: 
> >
> >  $ go doc os.File.Seek 
> >
> thanks all,   I use ide go to definition,  it go to the file_unix.go
> in this file seek  is a private method.
> So  I am confused 

That's the problem with your IDE.
Consider reporting this bug to its developers.

The `go doc` command only deals with exported symbols, so when you use
it, you're on the safe side.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] how to seek in file using go

2017-01-18 Thread hui zhang
so the file seek does not work in bufio.Read  ?

在 2017年1月19日星期四 UTC+8下午2:59:39,hui zhang写道:
>
> fi.Seek(0, os.SEEK_SET )?
>
>
> I set this in the code  and I expected to print 3.13 twice , but this code 
> print 3.13 3.14
> why?
>
> package main
>
> import (
>_ "bytes"
>"fmt"
>_ "math"
>
>"bufio"
>"encoding/binary"
>"os"
> )
>
> func main() {
>var pi float64
>//b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
>// 0x78, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
>//buf := bytes.NewReader(b)
>//err := binary.Read(buf, binary.LittleEndian, )
>//if err != nil {
>// fmt.Println("binary.Read failed:", err)
>//}
>//fmt.Println(pi)
> Output: 3.141592653589793
>//err = binary.Read(buf, binary.LittleEndian, )
>//if err != nil {
>// fmt.Println("binary.Read failed:", err)
>//}
>//fmt.Println(pi)
> //
>// open output file
>fo, err := os.Create("./output.bin")
>if err != nil {
>   panic(err)
>}
>
>// make a write buffer
>var fl1 float64 = 3.13
>var fl2 float64 = 3.14
>w := bufio.NewWriter(fo)
>err = binary.Write(w, binary.LittleEndian, fl1)
>if err != nil {
>   fmt.Println("binary.Write failed:", err)
>}
>err = binary.Write(w, binary.LittleEndian, fl2)
>if err != nil {
>   fmt.Println("binary.Write failed:", err)
>}
>w.Flush()
>// close fo on exit and check for its returned error
>if err := fo.Close(); err != nil {
>   panic(err)
>}
>
>//
>fi, err := os.Open("./output.bin")
>if err != nil {
>   panic(err)
>}
>// close fi on exit and check for its returned error
>defer func() {
>   if err := fi.Close(); err != nil {
>  panic(err)
>   }
>}()
>
>rd := bufio.NewReader(fi)
>err = binary.Read(rd, binary.LittleEndian, )
>if err != nil {
>   fmt.Println("binary.Read failed:", err)
>}
>fmt.Println(pi)
>
>fi.Seek(0, os.SEEK_SET )?
>
>err = binary.Read(rd, binary.LittleEndian, )
>if err != nil {
>   fmt.Println("binary.Read failed:", err)
>}
>fmt.Println(pi)
>
> }
>
>
> 在 2017年1月19日星期四 UTC+8下午2:43:31,Ayan George写道:
>>
>>
>>
>> On 01/19/2017 01:22 AM, hui zhang wrote: 
>> > I am using encoding/binary to read/write (struct)data to/from file. 
>> > Some times , I need to seek in the file while reading . 
>> > how to do this in go. 
>> > Check the code below 
>>
>> [snip!] 
>> > 
>> > 
>> > fi, err := os.Open("./output.bin") 
>> >if err != nil { 
>> >   panic(err) 
>> >} 
>> >// close fi on exit and check for its returned error 
>> > defer func() { 
>> >   if err := fi.Close(); err != nil { 
>> >  panic(err) 
>> >   } 
>> >}() 
>> > 
>> >rd := bufio.NewReader(fi) 
>> >err = binary.Read(rd, binary.LittleEndian, ) 
>> >if err != nil { 
>> >   fmt.Println("binary.Read failed:", err) 
>> >} 
>> >fmt.Println(pi) 
>> > 
>> >fi.seek()? 
>> > 
>>
>> Is File.Seek() what you're looking for or do you need something else? 
>>
>>   https://golang.org/pkg/os/#File.Seek 
>>
>> -ayan 
>>
>>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] how to seek in file using go

2017-01-18 Thread hui zhang


fi.Seek(0, os.SEEK_SET )?


I set this in the code  and I expected to print 3.13 twice , but this code 
print 3.13 3.14
why?

package main

import (
   _ "bytes"
   "fmt"
   _ "math"

   "bufio"
   "encoding/binary"
   "os"
)

func main() {
   var pi float64
   //b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
   // 0x78, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
   //buf := bytes.NewReader(b)
   //err := binary.Read(buf, binary.LittleEndian, )
   //if err != nil {
   // fmt.Println("binary.Read failed:", err)
   //}
   //fmt.Println(pi)
    Output: 3.141592653589793
   //err = binary.Read(buf, binary.LittleEndian, )
   //if err != nil {
   // fmt.Println("binary.Read failed:", err)
   //}
   //fmt.Println(pi)
//
   // open output file
   fo, err := os.Create("./output.bin")
   if err != nil {
  panic(err)
   }

   // make a write buffer
   var fl1 float64 = 3.13
   var fl2 float64 = 3.14
   w := bufio.NewWriter(fo)
   err = binary.Write(w, binary.LittleEndian, fl1)
   if err != nil {
  fmt.Println("binary.Write failed:", err)
   }
   err = binary.Write(w, binary.LittleEndian, fl2)
   if err != nil {
  fmt.Println("binary.Write failed:", err)
   }
   w.Flush()
   // close fo on exit and check for its returned error
   if err := fo.Close(); err != nil {
  panic(err)
   }

   //
   fi, err := os.Open("./output.bin")
   if err != nil {
  panic(err)
   }
   // close fi on exit and check for its returned error
   defer func() {
  if err := fi.Close(); err != nil {
 panic(err)
  }
   }()

   rd := bufio.NewReader(fi)
   err = binary.Read(rd, binary.LittleEndian, )
   if err != nil {
  fmt.Println("binary.Read failed:", err)
   }
   fmt.Println(pi)

   fi.Seek(0, os.SEEK_SET )?

   err = binary.Read(rd, binary.LittleEndian, )
   if err != nil {
  fmt.Println("binary.Read failed:", err)
   }
   fmt.Println(pi)

}


在 2017年1月19日星期四 UTC+8下午2:43:31,Ayan George写道:
>
>
>
> On 01/19/2017 01:22 AM, hui zhang wrote: 
> > I am using encoding/binary to read/write (struct)data to/from file. 
> > Some times , I need to seek in the file while reading . 
> > how to do this in go. 
> > Check the code below 
>
> [snip!] 
> > 
> > 
> > fi, err := os.Open("./output.bin") 
> >if err != nil { 
> >   panic(err) 
> >} 
> >// close fi on exit and check for its returned error 
> > defer func() { 
> >   if err := fi.Close(); err != nil { 
> >  panic(err) 
> >   } 
> >}() 
> > 
> >rd := bufio.NewReader(fi) 
> >err = binary.Read(rd, binary.LittleEndian, ) 
> >if err != nil { 
> >   fmt.Println("binary.Read failed:", err) 
> >} 
> >fmt.Println(pi) 
> > 
> >fi.seek()? 
> > 
>
> Is File.Seek() what you're looking for or do you need something else? 
>
>   https://golang.org/pkg/os/#File.Seek 
>
> -ayan 
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] how to seek in file using go

2017-01-18 Thread hui zhang
thanks all,   I use ide go to definition,  it go to the file_unix.go
in this file seek  is a private method.
So  I am confused 

在 2017年1月19日星期四 UTC+8下午2:45:16,Konstantin Khomoutov写道:
>
> On Wed, 18 Jan 2017 22:22:48 -0800 (PST) 
> hui zhang  wrote: 
>
> [...] 
> >fi, err := os.Open("./output.bin") 
> [...] 
> >fi.seek()? 
>
> The usual drill is: 
>
> 1. Get the documentation on os.Open(): 
>
>  $ go doc os.Open 
>
>Notice what it returns in its 1st return value has the 
>type *File.  Since it has no "pkg." qualifier it means 
>that name is defined in the same package. 
>
> 2. So we now read the docs on os.File: 
>
>  $ go doc os.File 
>
>...and see all of its methods there, inclusing Seek(). 
>
> 3. So we read the docs on it: 
>
>  $ go doc os.File.Seek 
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] how to seek in file using go

2017-01-18 Thread Konstantin Khomoutov
On Wed, 18 Jan 2017 22:22:48 -0800 (PST)
hui zhang  wrote:

[...]
>fi, err := os.Open("./output.bin")
[...]
>fi.seek()?

The usual drill is:

1. Get the documentation on os.Open():

 $ go doc os.Open

   Notice what it returns in its 1st return value has the
   type *File.  Since it has no "pkg." qualifier it means
   that name is defined in the same package.

2. So we now read the docs on os.File:

 $ go doc os.File

   ...and see all of its methods there, inclusing Seek().

3. So we read the docs on it:

 $ go doc os.File.Seek

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] how to seek in file using go

2017-01-18 Thread Ayan George


On 01/19/2017 01:22 AM, hui zhang wrote:
> I am using encoding/binary to read/write (struct)data to/from file.
> Some times , I need to seek in the file while reading .
> how to do this in go.
> Check the code below

[snip!]
>
>
> fi, err := os.Open("./output.bin")
>if err != nil {
>   panic(err)
>}
>// close fi on exit and check for its returned error
> defer func() {
>   if err := fi.Close(); err != nil {
>  panic(err)
>   }
>}()
>
>rd := bufio.NewReader(fi)
>err = binary.Read(rd, binary.LittleEndian, )
>if err != nil {
>   fmt.Println("binary.Read failed:", err)
>}
>fmt.Println(pi)
>
>fi.seek()?
>

Is File.Seek() what you're looking for or do you need something else?

  https://golang.org/pkg/os/#File.Seek

-ayan

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] how to seek in file using go

2017-01-18 Thread hui zhang
I am using encoding/binary to read/write (struct)data to/from file.
Some times , I need to seek in the file while reading .
how to do this in go. 
Check the code below



package main

import (
   _ "bytes"
   "fmt"
   _ "math"

   "bufio"
   "encoding/binary"
   "os"
)

func main() {
   var pi float64
   //b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
   // 0x78, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
   //buf := bytes.NewReader(b)
   //err := binary.Read(buf, binary.LittleEndian, )
   //if err != nil {
   // fmt.Println("binary.Read failed:", err)
   //}
   //fmt.Println(pi)
    Output: 3.141592653589793
   //err = binary.Read(buf, binary.LittleEndian, )
   //if err != nil {
   // fmt.Println("binary.Read failed:", err)
   //}
   //fmt.Println(pi)
//
   // open output file
   fo, err := os.Create("./output.bin")
   if err != nil {
  panic(err)
   }

   // make a write buffer
   var fl1 float64 = 3.13
   var fl2 float64 = 3.14
   w := bufio.NewWriter(fo)
   err = binary.Write(w, binary.LittleEndian, fl1)
   if err != nil {
  fmt.Println("binary.Write failed:", err)
   }
   err = binary.Write(w, binary.LittleEndian, fl2)
   if err != nil {
  fmt.Println("binary.Write failed:", err)
   }
   w.Flush()
   // close fo on exit and check for its returned error
   if err := fo.Close(); err != nil {
  panic(err)
   }

   //
   fi, err := os.Open("./output.bin")
   if err != nil {
  panic(err)
   }
   // close fi on exit and check for its returned error
   defer func() {
  if err := fi.Close(); err != nil {
 panic(err)
  }
   }()

   rd := bufio.NewReader(fi)
   err = binary.Read(rd, binary.LittleEndian, )
   if err != nil {
  fmt.Println("binary.Read failed:", err)
   }
   fmt.Println(pi)

   fi.seek()?

   err = binary.Read(rd, binary.LittleEndian, )
   if err != nil {
  fmt.Println("binary.Read failed:", err)
   }
   fmt.Println(pi)

}

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: [golang-dev] Binding receiver object to a method expression, to get a method value

2017-01-18 Thread minux
On Wed, Jan 18, 2017 at 9:55 PM, Bryan Chan  wrote:

> On Wednesday, 18 January 2017 16:39:03 UTC-5, rog wrote:
>>
>> On 18 January 2017 at 18:45, Bryan Chan  wrote:
>> > But I couldn't find a way to bind a receiver to a method expression,
>> such
>> > that the resulting method value can be invoked at a later time. Would
>> this
>> > be a useful addition to the language? Or did I miss something?
>>
>> If a is an object with the method foo, a.foo will give you a function
>> that
>> will invoke the method at a later time.
>>
>> See https://golang.org/ref/spec#Method_values
>>
>
> I understand what method values are. My original email already contained
> an example. What I want to do is to store away a method expression, and
> then over time, bind that expression with different receivers to form
> different method values, so that those method values can be passed to other
> parts of the program and invoked later.
>

It's possible to use reflect.MakeFunc to bind a argument to a func.
The result won't be very efficient though, so unless you really want to be
generic, you probably should create a closure for this statically (which
means you must know the exact type of the method).

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: [golang-dev] Binding receiver object to a method expression, to get a method value

2017-01-18 Thread Tamás Gulácsi
No, but you can create a function/closure which gets the receiver as first arg, 
and either executes the method with the given args, or returns such a function, 
as you wish.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Bufio vs. Writing to Disk

2017-01-18 Thread Tamás Gulácsi
The bufio.NewWriter is unneeded here, as both the gzip.Writer and csv.Writer 
are buffered.

The concept of csv.Writer(gzip.Writer(os.File)) is ok.

Compression is cpu bound, and csv.Writer may need some optimization.

But first measure, use testing package's benchmark functionality.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: [golang-dev] Binding receiver object to a method expression, to get a method value

2017-01-18 Thread Bryan Chan
On Wednesday, 18 January 2017 16:39:03 UTC-5, rog wrote:
>
> On 18 January 2017 at 18:45, Bryan Chan  
> wrote: 
> > But I couldn't find a way to bind a receiver to a method expression, 
> such 
> > that the resulting method value can be invoked at a later time. Would 
> this 
> > be a useful addition to the language? Or did I miss something? 
>
> If a is an object with the method foo, a.foo will give you a function that 
> will invoke the method at a later time. 
>
> See https://golang.org/ref/spec#Method_values 
>
 
I understand what method values are. My original email already contained an 
example. What I want to do is to store away a method expression, and then 
over time, bind that expression with different receivers to form different 
method values, so that those method values can be passed to other parts of 
the program and invoked later.

--
Bryan

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Is there any import path limitation when using go:linkname localname importpath.name?

2017-01-18 Thread Cholerae Hu
I'm trying to play with go:linkname, and I wrote a demo for this.

directory structure:

[test]$ tree
.
├── main.go
├── pri
│   └── a.go
└── pub
├── issue15006.s
└── b.go

a.go:

package pri 


import (
"fmt"
)
func rua() int64 {
fmt.Println("rua in pri")
return 1
}

b.go:

package pub 


import (
"unsafe"
)


var _ = unsafe.Sizeof(0)


//go:noescape
//go:linkname rua test/pri.rua
func rua() int64


func Rua() int64 {
return rua()
}

main.go:

package main


import (
"test/pub"
)


func main() {
println(pub.Rua())
}

issue15006.s is a workaround for issue 15006 
 and it's totally empty.

Than I ran `go run main.go` and got an error:

# command-line-arguments
test/pub.Rua: test/pri.rua: not defined
test/pub.Rua: undefined: test/pri.rua

So what's wrong with this demo?

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: encoding/gob decode []byte to multi struct types

2017-01-18 Thread caspian46
Thanks for you reply, I got it done: https://play.golang.org/p/oOEELLyaVv
This is how I did it, pass *interface{} to gob.Encode, not interface{}  
  the same with gob.Decode, then get the type info from the decoded value 
interface{}

在 2017年1月18日星期三 UTC+8下午10:51:51,Jordan Krage写道:
>
> Actually, it looks like the int/int8 distinction doesn't matter for gob.
>
>> There is no int8, int16 etc. discrimination in the gob format; there are 
>> only signed and unsigned integers.
>>
> https://golang.org/pkg/encoding/gob/ 
>
> On Wednesday, January 18, 2017 at 8:43:27 AM UTC-6, Jordan Krage wrote:
>>
>> How about a compromise with denser type info? (could consider an int8 as 
>> well) 
>>  
>> type infoType int
>>
>> const (
>> firstType infoType = iota
>> secondType
>> ...
>> )
>>
>> type Info struct{
>> infoType
>> IData interface{}
>> }
>>
>>
>> On Wednesday, January 18, 2017 at 12:07:49 AM UTC-6, casp...@gmail.com 
>> wrote:
>>>
>>> First thanks for you reply. 
>>> Because in fact, I have about more than 10 different struct types (maybe 
>>> more and more), your suggest is good, this is better
>>> type Info struct{
>>> typeStr string // store the type info to decode IData
>>> IData interface{}
>>> }
>>>
>>> But I don't like to send the typeInfo through rpc call(I'm using gob to 
>>> do rpc call),maybe there's a better way to do it, just I haven't found it。
>>>
>>> 在 2017年1月18日星期三 UTC+8上午11:12:05,Felipe Spinolo写道:

 Why not just create a wrapper struct? Something like:

 type Mixed struct{
 A *A
 B *B
 }

 And then just encode/decode that?

 On Tuesday, January 17, 2017 at 1:16:37 PM UTC-8, casp...@gmail.com 
 wrote:
>
> I got two struct types encoded into bytes by gob, I want to decode the 
> bytes into a specific struct, is it possible without try all struct 
> types(maybe 3 or more struct types)?
>
>  I want to decode bytes without specify which elem in mixedBytes is 
> struct A, and which is struct B.
> This is the playground url:  https://play.golang.org/p/F0Wp5eWdGu
>
> Is is possible to implement it by a map or something like this?
>
> var typesMap = map[string]reflect.Value{
> "A": reflect.ValueOf(A{}),
> "B": reflect.ValueOf(B{}),
> }
>
>
>
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] exec.Command("cp", "-r", "./*.json", artifact.dir fails with exit status 1

2017-01-18 Thread ๏̯͡๏
Thanks for time in responding.

Appreciate.

On Wed, Jan 18, 2017 at 1:46 PM, roger peppe  wrote:

>
>
> On 17 January 2017 at 19:14, Deepak Jain  wrote:
>
>> I fixed the error by invoking Copy command with abs source path.
>>
>> srcPath, _ :=
>> e := util.Copy(srcPath, dst)
>>
>> If i could modify into a single line
>>
>> util.Copy(filepath.Abs(filepath.Dir(src)), dst)
>> Throws error as filepath.Abs(filepath.Dir(src)) returns a tuple.
>>
>
> It should not be necessary to use an absolute path.
>
>
>>
>> In scala i can use _1, _2 to access elements within a tuple. any
>> suggestions in Go?
>>
>
> As Matt says, it's not good practice to ignore errors in Go. It's not
> always possible
> to obtain the current directory and you really want to know if it's failed
> to do so.
>
>
>>
>> On Tuesday, January 17, 2017 at 10:50:51 AM UTC-8, Deepak Jain wrote:
>>>
>>> Thanks for pointing to https://github.com/juju/uti
>>> ls/blob/master/fs/copy.go
>>>
>>>
>>> I was testing Copy function that recursively copies directories. I am
>>> able to copy first level of files and it creates directories. It fails to
>>> copy the contents within those directories.
>>>
>>> func Copy(src, dst string) error {...}
>>>
>>> Error
>>>
>>> $ triggerCopyCommand.go
>>> lstat sd101-jvm/target/*.jar: no such file or directory ---> This is OK.
>>> As it does not exists.
>>> lstat sd101-python/sd101/*: no such file or directory  ---> This exists
>>> in the directory from which Copy is invoked.
>>> lstat sd101-r/sd101/*: no such file or directory   ---> This exists.
>>> lstat sd101-shell/sd101/*: no such file or directory ---> This exists.
>>>
>>> $ ls sd101-python/sd101/
>>> __init__.py prepare setup.py train
>>> $
>>>
>>> $ ls sd101-r/sd101/
>>> helloWorld.R
>>> $ ls sd101-shell/sd101/
>>> ls: sd101-shell/sd101/: No such file or directory
>>> $
>>>
>>>
>>> Any suggestions ?
>>>
>>>
>>> Can this code be replicated https://github.com/
>>> juju/utils/blob/master/fs/copy.go ? Or do you recommend using it as a
>>> library ?
>>>
>>> On Tuesday, January 17, 2017 at 2:12:29 AM UTC-8, rog wrote:

 You don't really need to use the external cp command:
 https://play.golang.org/p/F1YHzQL4UN


 On 16 January 2017 at 21:35, Deepak Jain  wrote:
 > util.ExecuteCommandWithOuput(exec.Command("cp", "-r", "./*.json",
 > artifact.dir))
 >
 > func ExecuteCommandWithOuput(cmd *exec.Cmd) {
 > output, err := cmd.Output()
 > if err != nil {
 > log.Print("Error executing ", cmd.Args, err)
 > }
 > fmt.Print(string(output))
 > }
 >
 >
 > Output
 >
 > 2017/01/16 13:26:35 Error executing [cp -r ./*.json myartifact] exit
 status
 > 1
 >
 >
 > Questions
 > 1. How do i get details of complete error message on failure of cp
 command ?
 > I did have err != nill and Print err
 > 2. Does exec.Command not support copy of files and recursive copy of
 > directories ?
 > 3. Any suggestions how i implement copy of files and recursive copy
 of
 > directories ?
 >
 > I have just started adopting Go and hence a new comer with Go.
 > Regards,
 > Deepak
 >
 > --
 > 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.

>>> --
>> 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+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>


-- 
Deepak

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] [ANN] Go CMS with automatic JSON API for "thick client" front-ends. Feedback appreciated. (req. Go 1.8)

2017-01-18 Thread steve manuel


With more and more sites / apps built with the "thick-client" mindset 
(SPAs, React/React Native, etc) I found that I needed something like 
Wordpress for a great CMS, but for JSON clients, not HTML pages. I wanted 
something like Rails CLI for really fast dev experience, but with better 
concurrency than Ruby.


So I built Ponzu, a project that I hope will help people build backends for 
their content-driven sites and apps: https://github.com/ponzu-cms/ponzu

   - Automatic & Free SSL/TLS
   - HTTP/2 and Server Push
   - Rapid development with CLI-controlled code generators
   - User-friendly, extensible CMS and administration dashboard
   - Simple deployment - single binary + assets, embedded DB (BoltDB 
   )
   - 
   
   Fast, helpful framework while maintaining control
   
   $ go1.8rc1 get github.com/ponzu-cms/ponzu/...
   
Looking for feedback before I record a video walk-through and re-post later 
this week or next.


p.s. check the wiki  for some 
documentation on the various interfaces you can implement on your content 
structs and get lots of control and added features: 
https://github.com/ponzu-cms/ponzu/wiki


Thanks!


screenshot: 




-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Bufio vs. Writing to Disk

2017-01-18 Thread Frank Davidson
Hi,

I have some code to write out a .csv file and then gzip it. I am using a 
bufio.NewWriter to link the two:

 x := gzip.NewWriter(outputFile)

 outBuf := bufio.NewWriter(x)

 w := csv.NewWriter(outBuf)


It seems slow. 

Would I be better off just writing first a .csv file to disk and then 
gzipping it in a separate operation and, if so, why? I would have expected 
the bufio.NewWriter to be faster because it keeps things in memory rather 
than on the disk.

Thanks!

Frank

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Marshal more than one XML line?

2017-01-18 Thread julian . klode


On Wednesday, 18 January 2017 06:07:00 UTC+1, Tomi Häsä wrote:
>
> What is the difference between Encoder and MarshalIndent? They both write 
> to a stream. When do you use Encoder?
>

MarshallIndent is a shortcut that creates an encoder, calls 
encoder.Indent() and then calls encoder.Encode():


func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
var b bytes.Buffer
enc := NewEncoder()
enc.Indent(prefix, indent)
if err := enc.Encode(v); err != nil {
return nil, err
}
return b.Bytes(), nil
}

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] exec.Command("cp", "-r", "./*.json", artifact.dir fails with exit status 1

2017-01-18 Thread roger peppe
On 17 January 2017 at 19:14, Deepak Jain  wrote:

> I fixed the error by invoking Copy command with abs source path.
>
> srcPath, _ :=
> e := util.Copy(srcPath, dst)
>
> If i could modify into a single line
>
> util.Copy(filepath.Abs(filepath.Dir(src)), dst)
> Throws error as filepath.Abs(filepath.Dir(src)) returns a tuple.
>

It should not be necessary to use an absolute path.


>
> In scala i can use _1, _2 to access elements within a tuple. any
> suggestions in Go?
>

As Matt says, it's not good practice to ignore errors in Go. It's not
always possible
to obtain the current directory and you really want to know if it's failed
to do so.


>
> On Tuesday, January 17, 2017 at 10:50:51 AM UTC-8, Deepak Jain wrote:
>>
>> Thanks for pointing to https://github.com/juju/uti
>> ls/blob/master/fs/copy.go
>>
>>
>> I was testing Copy function that recursively copies directories. I am
>> able to copy first level of files and it creates directories. It fails to
>> copy the contents within those directories.
>>
>> func Copy(src, dst string) error {...}
>>
>> Error
>>
>> $ triggerCopyCommand.go
>> lstat sd101-jvm/target/*.jar: no such file or directory ---> This is OK.
>> As it does not exists.
>> lstat sd101-python/sd101/*: no such file or directory  ---> This exists
>> in the directory from which Copy is invoked.
>> lstat sd101-r/sd101/*: no such file or directory   ---> This exists.
>> lstat sd101-shell/sd101/*: no such file or directory ---> This exists.
>>
>> $ ls sd101-python/sd101/
>> __init__.py prepare setup.py train
>> $
>>
>> $ ls sd101-r/sd101/
>> helloWorld.R
>> $ ls sd101-shell/sd101/
>> ls: sd101-shell/sd101/: No such file or directory
>> $
>>
>>
>> Any suggestions ?
>>
>>
>> Can this code be replicated https://github.com/
>> juju/utils/blob/master/fs/copy.go ? Or do you recommend using it as a
>> library ?
>>
>> On Tuesday, January 17, 2017 at 2:12:29 AM UTC-8, rog wrote:
>>>
>>> You don't really need to use the external cp command:
>>> https://play.golang.org/p/F1YHzQL4UN
>>>
>>>
>>> On 16 January 2017 at 21:35, Deepak Jain  wrote:
>>> > util.ExecuteCommandWithOuput(exec.Command("cp", "-r", "./*.json",
>>> > artifact.dir))
>>> >
>>> > func ExecuteCommandWithOuput(cmd *exec.Cmd) {
>>> > output, err := cmd.Output()
>>> > if err != nil {
>>> > log.Print("Error executing ", cmd.Args, err)
>>> > }
>>> > fmt.Print(string(output))
>>> > }
>>> >
>>> >
>>> > Output
>>> >
>>> > 2017/01/16 13:26:35 Error executing [cp -r ./*.json myartifact] exit
>>> status
>>> > 1
>>> >
>>> >
>>> > Questions
>>> > 1. How do i get details of complete error message on failure of cp
>>> command ?
>>> > I did have err != nill and Print err
>>> > 2. Does exec.Command not support copy of files and recursive copy of
>>> > directories ?
>>> > 3. Any suggestions how i implement copy of files and recursive copy of
>>> > directories ?
>>> >
>>> > I have just started adopting Go and hence a new comer with Go.
>>> > Regards,
>>> > Deepak
>>> >
>>> > --
>>> > 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.
>>>
>> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: [golang-dev] Binding receiver object to a method expression, to get a method value

2017-01-18 Thread roger peppe
On 18 January 2017 at 18:45, Bryan Chan  wrote:
> In Go, you could pass a method value around so that the method can be
> invoked later on a pre-determined receiver, e.g.
>
> func (a *A) foo() bool { ... }
>
> func bar(f func () bool) {
> if f() {
> ...
> }
> }
>
> func main() {
> a := { ... }
> bar(a.foo)
> }
>
>
> You could also create a method expression so that the receiver can be
> supplied at actual call time, e.g.
>
> f := (*A).foo
> boolVal := f(a)
>
>
> But I couldn't find a way to bind a receiver to a method expression, such
> that the resulting method value can be invoked at a later time. Would this
> be a useful addition to the language? Or did I miss something?

If a is an object with the method foo, a.foo will give you a function that
will invoke the method at a later time.

See https://golang.org/ref/spec#Method_values

>
> --
> Bryan
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-dev+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: [golang-dev] Is there current support for Cloud Endpoints in Go

2017-01-18 Thread Jaana Burcu Dogan
+golang-nuts, bcc: golang-dev

On Tue, Jan 17, 2017 at 2:38 PM, Brad Fitzpatrick 
wrote:

> https://groups.google.com/forum/#!forum/google-appengine-go looks like
> the right place.
>
>
> On Tue, Jan 17, 2017 at 2:30 PM, Dewey Gaedcke  wrote:
>
>> Yesthat project has not been updated in 7 months and does not seem to
>> work with the latest endpoints
>> It's hard for me to imagine that Google advertises being able to use Go
>> with Endpoints on App Engine and yet does not support it.
>> We selected Go with the understanding that we could generate our client
>> lib from our API.if we have to throw this away and start over in a
>> different language, it's a serious loss for us.
>>
>> Can you recommend a group or site where this question would be more
>> relevant?
>> Thanks for your response!!
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-dev" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-dev+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-dev+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] [ANN] go-funk, a modern Go utility library which provides helpers (map, find, contains, filter, ...)

2017-01-18 Thread Florent Messa
Hi,

go-funk has been released (https://github.com/thoas/go-funk), the 0.1 has 
been tagged.

This library provides multiple generic implementations:

   - Map
   - Contains
   - Keys
   - Values
   - Filter
   - Find
   - etc.

and also typesafe implementations.

More helpers are coming in the next few weeks.

Feel free to contribute and give your feedback.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Help creating a github.com/mattn/go-gtk/gtk.Image from image.Image?

2017-01-18 Thread James Ralphs
Hi,

I'm trying to create an gtk.Image from a Go image.Image (or similar, 
image.RGBA would be fine too), and am stuck. It looks like I need to create 
a gdk.Pixbuf to feed to the GTK image widget, but I can't figure out how to 
use the gdk.Pixbuf, to create a useful picture.

All the examples of how to make a gtk.Image that I found use the 
gtk.NewImageFromFile(path), so although I could save the image.Image to 
disk and then read it back, I'd obviously prefer not to.

Any examples, code samples or hints very welcome! Thanks!

Packages in question:
Pixbuf: github.com/mattn/go-gtk/gdkpixbuf
GTK: github.com/mattn/go-gtk/gtk

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: encoding/gob decode []byte to multi struct types

2017-01-18 Thread Jordan Krage
Actually, it looks like the int/int8 distinction doesn't matter for gob.

> There is no int8, int16 etc. discrimination in the gob format; there are 
> only signed and unsigned integers.
>
https://golang.org/pkg/encoding/gob/ 

On Wednesday, January 18, 2017 at 8:43:27 AM UTC-6, Jordan Krage wrote:
>
> How about a compromise with denser type info? (could consider an int8 as 
> well) 
>  
> type infoType int
>
> const (
> firstType infoType = iota
> secondType
> ...
> )
>
> type Info struct{
> infoType
> IData interface{}
> }
>
>
> On Wednesday, January 18, 2017 at 12:07:49 AM UTC-6, casp...@gmail.com 
> wrote:
>>
>> First thanks for you reply. 
>> Because in fact, I have about more than 10 different struct types (maybe 
>> more and more), your suggest is good, this is better
>> type Info struct{
>> typeStr string // store the type info to decode IData
>> IData interface{}
>> }
>>
>> But I don't like to send the typeInfo through rpc call(I'm using gob to 
>> do rpc call),maybe there's a better way to do it, just I haven't found it。
>>
>> 在 2017年1月18日星期三 UTC+8上午11:12:05,Felipe Spinolo写道:
>>>
>>> Why not just create a wrapper struct? Something like:
>>>
>>> type Mixed struct{
>>> A *A
>>> B *B
>>> }
>>>
>>> And then just encode/decode that?
>>>
>>> On Tuesday, January 17, 2017 at 1:16:37 PM UTC-8, casp...@gmail.com 
>>> wrote:

 I got two struct types encoded into bytes by gob, I want to decode the 
 bytes into a specific struct, is it possible without try all struct 
 types(maybe 3 or more struct types)?

  I want to decode bytes without specify which elem in mixedBytes is 
 struct A, and which is struct B.
 This is the playground url:  https://play.golang.org/p/F0Wp5eWdGu

 Is is possible to implement it by a map or something like this?

 var typesMap = map[string]reflect.Value{
 "A": reflect.ValueOf(A{}),
 "B": reflect.ValueOf(B{}),
 }





-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Applying idiomatic Golang patterns to other languages?

2017-01-18 Thread omarshariffdontlikeit
I experimented using the function options (e.g. withFunc() ) in PHP. I know 
its unnecessary as PHP functions and methods can accept optional 
parameters, and I know that you cant have function types in PHP, but the 
approach is still so clean and self documenting I'd like to use it for much 
more in PHP:

$conn = new Beanstalk\Connection(
withHost('127.0.0.1'),
withPort(11300),
);

I've also found that my approach is much more "inside out", much more like 
Uncle Bobs clean architecture - a core library of domain objects is what I 
am building, with supporting tools for use cases e.g. PHAR command line 
tool.


On Tuesday, January 17, 2017 at 5:58:46 AM UTC, so.q...@gmail.com wrote:
>
> Just curious how often you find yourself applying idiomatic Go patterns to 
> other languages? (JavaScript, Python, C#, Java)
>
> For instance returning and handling an error value as opposed to 
> throw-try-catch. I understand this isn't the best example since try-catch 
> exceptions are more closely aligned to panics, but I do find myself 
> returning more error values since using Go.
>
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.