[go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread snmed
Hi Bryan

But the returned uintptr from syscall is as far as i know a pointer to a 
struct which has been created by the C API, so go runtime won't touch it or 
do I miss something?

Am Freitag, 15. Dezember 2017 00:48:51 UTC+1 schrieb Bryan Mills:
>
> In this case, the vet tool is correct if you're making the syscall with 
> Go-allocated memory.
> The Go runtime is allowed to move values around: the address of a Go 
> variable is only pinned for the duration of the syscall itself.
>
> If you've got C-allocated memory (or statically-allocated memory), 
> https://golang.org/issue/13656#issuecomment-303216308 has a solution that 
> avoids copying and is more robust to large sizes.
>
>
> On Thursday, December 14, 2017 at 5:27:57 AM UTC-5, snmed wrote:
>>
>> Okay I found a way, this seems to work:
>>
>> ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:certctx.
>> cbCertEncoded]
>>
>> or
>>
>> mm := make([]byte, certctx.cbCertEncoded)
>> for i := uint32(0) ; i < certctx.cbCertEncoded; i++ {
>> mm[i] = *((*byte)(unsafe.Pointer(certctx.pbCertEncoded + uintptr(
>> i) * unsafe.Sizeof(new(byte)
>> }
>>
>>
>>
>> Anyway, the vet tool is complaining:
>>
>> main.go:106: possible misuse of unsafe.Pointer
>> main.go:109: possible misuse of unsafe.Pointer
>>
>> This is the code fragment:
>>
>> 104certctx := new(CERT_CONTEXT)
>> 105
>> 106certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
>> 107
>> 108
>> 109ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:
>> certctx.cbCertEncoded]
>>
>> Is there another way to use syscall return values uintptr without vet 
>> warnings? And which solution should I prefer? I think the later one should 
>> be more safe, isn't it?
>>
>> Cheers
>>
>>
>> Am Donnerstag, 14. Dezember 2017 09:29:38 UTC+1 schrieb snmed:
>>>
>>> Hi Miki 
>>>
>>> I'm using syscall package and no C import, but maybe it is possible to 
>>> use this function as well?
>>>
>>> Am Donnerstag, 14. Dezember 2017 09:18:26 UTC+1 schrieb Miki Tebeka:

 Do you mean

 func C.GoBytes(unsafe.Pointer, C.int) []byte

  ?

 On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:
>
> Hi all
>
> I'm trying to map a C structure to an equivalent go struct, but I 
> bumped into a problem with a pointer to byte that is actually an array of 
> bytes.
>
> Here is the C struct:
>
> typedef struct _CERT_CONTEXT {
>   DWORD  dwCertEncodingType;
>   BYTE   *pbCertEncoded;
>   DWORD  cbCertEncoded;
>   PCERT_INFO pCertInfo;
>   HCERTSTORE hCertStore;
> } CERT_CONTEXT, *PCERT_CONTEXT;
>
>
> and this is my go struct:
>
> type CERT_CONTEXT struct {
> dwCertEncodingType uint32
> pbCertEncoded  uintptr
> cbCertEncoded  uint32
> pCertInfo  uintptr
> hCertStore uintptr
> }
>
> for my case I need only the first 3 fields and I do not have any 
> problem to get 1 and 3, but I can't remember how to translate the second 
> field to a slice of bytes.
> This is how I map the struct from an uintptr and print it to the 
> console:
>
> certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
> fmt.Printf("%v\n", certctx)
> 
> >&{1 807520 674 833008 789360}
>
> Any advise is warmly welcome.
>
> Cheers,
> Sandro
>


-- 
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] Github wiki pages publicly editable

2017-12-14 Thread Ivan Borshukov
Forwarding to the mailing list this time.

Cheers.

On Dec 14, 2017 18:14, "Ivan Borshukov"  wrote:

Ok, thanks.

If so, I want to announce the change that I've maid. It is regarding the
rate limiting section [0].

The wiki page states that when the created ticker is Stop-ed, the created
goroutine will exit,
which I think is not true, since the Ticker.Stop's documentation[1] states
that Stop does not
result in Ticker.C being closed. A minimal example confirms my
understanding and results
in a panic due to deadlock.

```go
package main

import (
"sync"
"time"
)

func main() {
tick := time.NewTicker(time.Second)
throttle := make(chan time.Time, 1)

wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for t := range tick.C {
select {
case throttle <- t:
default:
}
} // Wiki page says: exits after tick.Stop()
}()

tick.Stop()
wg.Wait()
}
```

The above code in the playground: https://play.golang.org/p/oWrcCa25ki.

The diff can be viewed at [2].


[0] https://github.com/golang/go/wiki/RateLimiting
[1] https://godoc.org/time#Ticker.Stop
[2] https://github.com/golang/go/wiki/RateLimiting/_compare/
1a0ca7c57e7845c9ff05d296b721b46afa7c4f9e...67bbbe92527c42753
b45775ac981e14d89b2b95e

Cheers,
Ivan

2017-12-14 18:06 GMT+02:00 Jan Mercl <0xj...@gmail.com>:

> On Thu, Dec 14, 2017 at 5:05 PM Ivan Borshukov 
> wrote:
>
> > I've noticed that I'm able to edit the wiki pages on
> https://github.com/golang/go/wiki and I wonder whether this is
> intentional.
>
> IINM, it's like that from the beginning of the Github repository, so
> probably intentional.
>
> --
>
> -j
>



-- 
Ivan Borshukov, bo0mer...@gmail.com

-- 
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] Use golang to call the win API

2017-12-14 Thread 流沙
API Address:https://msdn.microsoft.com/en-us/library/ms706716(VS.85).aspx

Code:

package main

// #define WIN32_LEAN_AND_MEAN
// #include 

import (
   "fmt"
   "syscall"
   "unsafe"
   "C"
)
var (
   wlankernal,_ = syscall.LoadLibrary("Wlanapi.dll")
   wlanhandle,_ = syscall.GetProcAddress(wlankernal,"WlanOpenHandle")
   wlanclosehandle,_ = syscall.GetProcAddress(wlankernal,"WlanCloseHandle")
   wlanenumInterfaces,_ = 
syscall.GetProcAddress(wlankernal,"WlanEnumInterfaces")
   wlangetprofile,_ = syscall.GetProcAddress(wlankernal,"WlanGetProfileList")
)
type ulong int32

type WLAN_INTERFACE_INFO  struct{
   InterfaceGuid syscall.GUID
   strInterfaceDescription string
   isState uint
}

type WLAN_INTERFACE_INFO_LIST struct {
   NumberOfItems uint32
   Index uint32
   InterfaceInfo WLAN_INTERFACE_INFO
}

type WLAN_PROFILE_INFO struct{
   ProfileName C.char
   Flags ulong
}

type WLAN_PROFILE_INFO_LIST struct{
   NumberOfItems ulong
   Index ulong
   ProfileInfo WLAN_PROFILE_INFO
}

func abort(funcname string, err error) {
   panic(fmt.Sprintf("%s failed: %v", funcname, err))
}

//打开一个WLAN句柄
func WlanOpenHandle() (result uint32)  {
   negotiated_version := uint32(0)
   client_handle := uint32(0)
   dwClientVersion := uint32(2)
   var nargs uintptr = 4
   ret,_,callErr := syscall.Syscall6(uintptr(wlanhandle),
  nargs,
  uintptr(dwClientVersion),
  0,
  uintptr(unsafe.Pointer(_version)),
  uintptr(unsafe.Pointer(_handle)),
  0,
  0,
   )
   if ret != 0{
  abort("StartWLANHandleError", callErr)
  return
   }
   result = uint32(client_handle)
   return
}

//关闭WLAN句柄
func WlanCloseHandle()  {
   var nargs uintptr = 2
   handle :=WlanOpenHandle()
   ret,_,callErr := syscall.Syscall(uintptr(wlanclosehandle),
  nargs,
  uintptr(handle),
  0,
  0,
   )
   if ret !=0{
  abort("CloseHandleError",callErr)
  return
   }
   fmt.Println("WlanCloseHandle Successful")
}


func main()  {
   defer syscall.FreeLibrary(wlankernal)
   var nargs uintptr = 3
   handle :=WlanOpenHandle();
   var wlan_interface_info WLAN_INTERFACE_INFO_LIST;
   ret,_,callErr := syscall.Syscall(uintptr(wlanenumInterfaces),
  nargs,
  uintptr(handle),
  0,
  uintptr(unsafe.Pointer(_interface_info)),
   )
   if ret !=0{
  abort("WlanEnumInterfacesError",callErr)
  return
   }
   WlanCloseHandle()
   fmt.Println(ret)
   fmt.Println(wlan_interface_info)
}



wlan_interface_info returns a value of {7306176 0 {{0 0 0 [0 0 0 0 0 0 0 
0]}  0}}
This value seems to be wrong!
May I ask how to solve this problem?

-- 
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] Storing types in a map[string]type ?

2017-12-14 Thread jakdept
I'm writing a utility to import/export data into an API. I'd like to be 
able to accept multiple input/output formats, and support multiple 
commands. I was going to use kingpin for the flags/commands, and do a type 
format after that.

My plan was to declare an interface with the methods I'm going to use, a 
struct that's got the parameters needed for the interface, but doesn't 
satisfy it, and declare a map of structs that do satisfy that interface.

type versionParams struct {
 options int
 sessapi.Session
 in  io.ReadCloser
 out io.WriteCloser
 log *log.Logger
}


type Version interface {
 Import()
 Export()
 Example()
}


var versions map[string]Version

In main, I'd detect the actions, and if it's one of the actions for one of 
those types, create the object, load it up with input and output and stuff. 
Note, because of the flags I'm using, *format is a pointer to a string 
holding the format's name. So, I was thinking I'd do something like:

args := versionParams{}


// some stuff to fill in the args


args.(versions[*format]).Import()
 
Then, for each version I want to add, in that version I'd do something like 
the following, in it's own file:

type v1csv struct {
 versionParams
 Version
}


func init() {
 versions["v1csv"] = v1csv
}


func (v *v1csv) Import() {
// do stuff
}


func (v *v1csv) Example() {
// do stuff
}


func (v *v1csv) Export() {
// do stuff
}

I don't think this works for multiple reasons though. I don't think you can 
declare a map of types that satisfy an interface like that? And once you 
do, I guess I don't know if I can cast/convert from one struct to another 
without directly naming the struct...? I guess I could drop the generic 
versionParams struct and just create structs for each type, but I would 
still want to put those struct types into a map - that's the part that I'm 
stuck on figuring out.

I wanted to go with something like this layout specifically so that I could 
add new formats by simply adding that one format in a self contained file. 
I don't want to statically name them and 

Mostly, I think I'm just lost in design and I cannot see the way out 
myself. Any suggestions to help me figure out how to do this, or something 
more appropriate, would be appreciated. :-)

-- 
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] Variadic assignable values and covariance

2017-12-14 Thread Jesse McNelis
On Fri, Dec 15, 2017 at 11:08 AM, Ivan Kurnosov  wrote:
> Jesse,
>
> what you quoted is an optimisation for the "Otherwise, the value passed is a
> new slice of type []T with a new underlying array whose successive elements
> ".
>
> Your quote says: if the final argument is assignable - it may be passed as
> is, without any copy involving.

I quoted the section that explains what the ... syntax does.
It's the only section in the spec that refers to that syntax.

The 'final argument' referred to here is the varadic argument of the function.

You can pass a slice as the value for a varadic argument by passing
the slice followed by ...

-- 
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] Variadic assignable values and covariance

2017-12-14 Thread Ivan Kurnosov
Jesse,

what you quoted is an optimisation for the "Otherwise, the value passed is
a new slice of type []T with a new underlying array whose successive
elements ".

Your quote says: if the final argument is assignable - it may be passed as
is, without any copy involving.


On 15 December 2017 at 12:59, Jesse McNelis  wrote:

> On Fri, Dec 15, 2017 at 8:18 AM, Ivan Kurnosov  wrote:
> > Why does this code not compile: https://play.golang.org/p/f5fMvO8Ns7
> > func f(items ...interface{}) {
> >  fmt.Println(items)
> > }
>
> The spec also says:
>
> "If the final argument is assignable to a slice type []T, it may be
> passed unchanged as the value for a ...T parameter if the argument is
> followed by  In this case no new slice is created."
>
> https://golang.org/ref/spec#Passing_arguments_to_..._parameters
>
> That is, you can pass in multiple arguments which will be combined in
> to the items parameters as a slice or tab... can be passed as the
> items parameter. You can't do both.
>



-- 
With best regards, Ivan Kurnosov

-- 
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: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread 'Bryan Mills' via golang-nuts
In this case, the vet tool is correct if you're making the syscall with 
Go-allocated memory.
The Go runtime is allowed to move values around: the address of a Go 
variable is only pinned for the duration of the syscall itself.

If you've got C-allocated memory (or statically-allocated 
memory), https://golang.org/issue/13656#issuecomment-303216308 has a 
solution that avoids copying and is more robust to large sizes.


On Thursday, December 14, 2017 at 5:27:57 AM UTC-5, snmed wrote:
>
> Okay I found a way, this seems to work:
>
> ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:certctx.
> cbCertEncoded]
>
> or
>
> mm := make([]byte, certctx.cbCertEncoded)
> for i := uint32(0) ; i < certctx.cbCertEncoded; i++ {
> mm[i] = *((*byte)(unsafe.Pointer(certctx.pbCertEncoded + uintptr(i
> ) * unsafe.Sizeof(new(byte)
> }
>
>
>
> Anyway, the vet tool is complaining:
>
> main.go:106: possible misuse of unsafe.Pointer
> main.go:109: possible misuse of unsafe.Pointer
>
> This is the code fragment:
>
> 104certctx := new(CERT_CONTEXT)
> 105
> 106certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
> 107
> 108
> 109ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:
> certctx.cbCertEncoded]
>
> Is there another way to use syscall return values uintptr without vet 
> warnings? And which solution should I prefer? I think the later one should 
> be more safe, isn't it?
>
> Cheers
>
>
> Am Donnerstag, 14. Dezember 2017 09:29:38 UTC+1 schrieb snmed:
>>
>> Hi Miki 
>>
>> I'm using syscall package and no C import, but maybe it is possible to 
>> use this function as well?
>>
>> Am Donnerstag, 14. Dezember 2017 09:18:26 UTC+1 schrieb Miki Tebeka:
>>>
>>> Do you mean
>>>
>>> func C.GoBytes(unsafe.Pointer, C.int) []byte
>>>
>>>  ?
>>>
>>> On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:

 Hi all

 I'm trying to map a C structure to an equivalent go struct, but I 
 bumped into a problem with a pointer to byte that is actually an array of 
 bytes.

 Here is the C struct:

 typedef struct _CERT_CONTEXT {
   DWORD  dwCertEncodingType;
   BYTE   *pbCertEncoded;
   DWORD  cbCertEncoded;
   PCERT_INFO pCertInfo;
   HCERTSTORE hCertStore;
 } CERT_CONTEXT, *PCERT_CONTEXT;


 and this is my go struct:

 type CERT_CONTEXT struct {
 dwCertEncodingType uint32
 pbCertEncoded  uintptr
 cbCertEncoded  uint32
 pCertInfo  uintptr
 hCertStore uintptr
 }

 for my case I need only the first 3 fields and I do not have any 
 problem to get 1 and 3, but I can't remember how to translate the second 
 field to a slice of bytes.
 This is how I map the struct from an uintptr and print it to the 
 console:

 certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
 fmt.Printf("%v\n", certctx)
 
 >&{1 807520 674 833008 789360}

 Any advise is warmly welcome.

 Cheers,
 Sandro

>>>

-- 
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] Variadic assignable values and covariance

2017-12-14 Thread Ivan Kurnosov
Why does this code not compile: https://play.golang.org/p/f5fMvO8Ns7

package main


import (
 "fmt"
)


func f(items ...interface{}) {
 fmt.Println(items)
}


func main() {
 f("a", "b", "c")
 tab := []interface{}{"d"}
 f("a", "b", "c", tab...)
}


The spec says:
> Otherwise, the value passed is a new slice of type []T with a new 
underlying array whose successive elements are the actual arguments, which 
all must be assignable  to T.

The elements are assignable. So what spec clause exactly prevents this from 
compile?

-- 
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] cache for *os.File

2017-12-14 Thread Dave Cheney
It depends a lot on what your application does, but I’d try the trace tool, 
which should give you execution times of your request and you see its 
interactions with the garbage collector.  For reasonable allocation rates you 
may find that the gc can collect in the background and not introduce additional 
latency.

-- 
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] Github wiki pages publicly editable

2017-12-14 Thread Jan Mercl
On Thu, Dec 14, 2017 at 5:05 PM Ivan Borshukov  wrote:

> I've noticed that I'm able to edit the wiki pages on
https://github.com/golang/go/wiki and I wonder whether this is intentional.

IINM, it's like that from the beginning of the Github repository, so
probably intentional.

-- 

-j

-- 
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] Github wiki pages publicly editable

2017-12-14 Thread Ivan Borshukov
Hello,

I've noticed that I'm able to edit the wiki pages 
on https://github.com/golang/go/wiki and I wonder whether this is 
intentional.

Cheers,
Ivan

-- 
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] go generate ignores files ignored for build

2017-12-14 Thread dc0d
IMHO //go:generate ... tasks inside a file marked by // +build ignore 
should run. Any file that is marked by // +build ignore will be ignored by 
the compiler. So your code (by putting // +build ignore inside main.go) 
will generate a compilation error.

On Thursday, December 14, 2017 at 2:41:47 PM UTC+3:30, Jan Mercl wrote:
>
> On Thu, Dec 14, 2017 at 11:53 AM dc0d  
> wrote:
>
> > What is the reasoning for not executing //go:generate ... comments 
> inside a file, that is excluded from build by // +build ignore? 
>
> 'ignore' is just a tag like any other:
>
> $ cat main.go 
> // +build ignore
>
> //go:generate echo foo
>
> package main
>
> func main() {}
> $ go generate
> $ go generate -tags ignore
> foo
> $ 
>
> -- 
>
> -j
>

-- 
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] Re: Learning Go: suggestions for code review?

2017-12-14 Thread Ben Hoyt
Yeah, I copied the go/token style there. However, thanks Nigel -- I didn't
realize you could do "renaming imports", that's nice. -Ben

On Thu, Dec 14, 2017 at 4:35 AM, Jan Mercl <0xj...@gmail.com> wrote:

> On Thu, Dec 14, 2017 at 6:27 AM Nigel Tao  wrote:
>
> > As per https://golang.org/doc/effective_go.html#mixed-caps ALL_CAPS
> > names are unusual Go style.
>
> They are, but token names are often/traditionally all caps and
> underscores: https://golang.org/pkg/go/token/#Token - in the old lex
> style. And it has a good reason, to distinguish terminals in a grammar.
> Even Go EBNF in the specification wants to make terminals stand out, even
> though using a different style, like in int_lit: https://golang.org/
> ref/spec#Operands.
>
>
>
> --
>
> -j
>

-- 
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] cache for *os.File

2017-12-14 Thread Vasiliy Tolstov
2017-12-14 9:28 GMT+03:00 Dave Cheney :
> Does your profiling suggest these allocations are causing latency?
>

Hmm this is missing part =). How can i understand what causing latency
if i use http prof?
-- 
Vasiliy Tolstov,
e-mail: v.tols...@selfip.ru

-- 
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] go generate ignores files ignored for build

2017-12-14 Thread Jan Mercl
On Thu, Dec 14, 2017 at 11:53 AM dc0d  wrote:

> What is the reasoning for not executing //go:generate ... comments inside
a file, that is excluded from build by // +build ignore?

'ignore' is just a tag like any other:

$ cat main.go
// +build ignore

//go:generate echo foo

package main

func main() {}
$ go generate
$ go generate -tags ignore
foo
$

-- 

-j

-- 
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] go generate ignores files ignored for build

2017-12-14 Thread dc0d
What is the reasoning for not executing //go:generate ... comments inside a 
file, that is excluded from build by // +build ignore?

-- 
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: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread snmed
Okay I found a way, this seems to work:

ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:certctx.
cbCertEncoded]

or

mm := make([]byte, certctx.cbCertEncoded)
for i := uint32(0) ; i < certctx.cbCertEncoded; i++ {
mm[i] = *((*byte)(unsafe.Pointer(certctx.pbCertEncoded + uintptr(i) 
* unsafe.Sizeof(new(byte)
}



Anyway, the vet tool is complaining:

main.go:106: possible misuse of unsafe.Pointer
main.go:109: possible misuse of unsafe.Pointer

This is the code fragment:

104certctx := new(CERT_CONTEXT)
105
106certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
107
108
109ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:
certctx.cbCertEncoded]

Is there another way to use syscall return values uintptr without vet 
warnings? And which solution should I prefer? I think the later one should 
be more safe, isn't it?

Cheers


Am Donnerstag, 14. Dezember 2017 09:29:38 UTC+1 schrieb snmed:
>
> Hi Miki 
>
> I'm using syscall package and no C import, but maybe it is possible to use 
> this function as well?
>
> Am Donnerstag, 14. Dezember 2017 09:18:26 UTC+1 schrieb Miki Tebeka:
>>
>> Do you mean
>>
>> func C.GoBytes(unsafe.Pointer, C.int) []byte
>>
>>  ?
>>
>> On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:
>>>
>>> Hi all
>>>
>>> I'm trying to map a C structure to an equivalent go struct, but I bumped 
>>> into a problem with a pointer to byte that is actually an array of bytes.
>>>
>>> Here is the C struct:
>>>
>>> typedef struct _CERT_CONTEXT {
>>>   DWORD  dwCertEncodingType;
>>>   BYTE   *pbCertEncoded;
>>>   DWORD  cbCertEncoded;
>>>   PCERT_INFO pCertInfo;
>>>   HCERTSTORE hCertStore;
>>> } CERT_CONTEXT, *PCERT_CONTEXT;
>>>
>>>
>>> and this is my go struct:
>>>
>>> type CERT_CONTEXT struct {
>>> dwCertEncodingType uint32
>>> pbCertEncoded  uintptr
>>> cbCertEncoded  uint32
>>> pCertInfo  uintptr
>>> hCertStore uintptr
>>> }
>>>
>>> for my case I need only the first 3 fields and I do not have any problem 
>>> to get 1 and 3, but I can't remember how to translate the second field to a 
>>> slice of bytes.
>>> This is how I map the struct from an uintptr and print it to the console:
>>>
>>> certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
>>> fmt.Printf("%v\n", certctx)
>>> 
>>> >&{1 807520 674 833008 789360}
>>>
>>> Any advise is warmly welcome.
>>>
>>> Cheers,
>>> Sandro
>>>
>>

-- 
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] Re: Learning Go: suggestions for code review?

2017-12-14 Thread Jan Mercl
On Thu, Dec 14, 2017 at 6:27 AM Nigel Tao  wrote:

> As per https://golang.org/doc/effective_go.html#mixed-caps ALL_CAPS
> names are unusual Go style.

They are, but token names are often/traditionally all caps and underscores:
https://golang.org/pkg/go/token/#Token - in the old lex style. And it has a
good reason, to distinguish terminals in a grammar. Even Go EBNF in the
specification wants to make terminals stand out, even though using a
different style, like in int_lit: https://golang.org/ref/spec#Operands.



-- 

-j

-- 
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: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread snmed
Hi Miki 

I'm using syscall package and no C import, but maybe it is possible to use 
this function as well?

Am Donnerstag, 14. Dezember 2017 09:18:26 UTC+1 schrieb Miki Tebeka:
>
> Do you mean
>
> func C.GoBytes(unsafe.Pointer, C.int) []byte
>
>  ?
>
> On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:
>>
>> Hi all
>>
>> I'm trying to map a C structure to an equivalent go struct, but I bumped 
>> into a problem with a pointer to byte that is actually an array of bytes.
>>
>> Here is the C struct:
>>
>> typedef struct _CERT_CONTEXT {
>>   DWORD  dwCertEncodingType;
>>   BYTE   *pbCertEncoded;
>>   DWORD  cbCertEncoded;
>>   PCERT_INFO pCertInfo;
>>   HCERTSTORE hCertStore;
>> } CERT_CONTEXT, *PCERT_CONTEXT;
>>
>>
>> and this is my go struct:
>>
>> type CERT_CONTEXT struct {
>> dwCertEncodingType uint32
>> pbCertEncoded  uintptr
>> cbCertEncoded  uint32
>> pCertInfo  uintptr
>> hCertStore uintptr
>> }
>>
>> for my case I need only the first 3 fields and I do not have any problem 
>> to get 1 and 3, but I can't remember how to translate the second field to a 
>> slice of bytes.
>> This is how I map the struct from an uintptr and print it to the console:
>>
>> certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
>> fmt.Printf("%v\n", certctx)
>> 
>> >&{1 807520 674 833008 789360}
>>
>> Any advise is warmly welcome.
>>
>> Cheers,
>> Sandro
>>
>

-- 
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: python remote objects

2017-12-14 Thread Miki Tebeka
Are you talking about https://pythonhosted.org/Pyro4/ ?

It really depends on the serialization format used. If it's json, msgpack 
or any other language independent format that you can do it. If it's Python 
specific (marshal, pickle, ...) then you can probably invoke some python 
code to convert from that format to one the Go can read - however some 
Python types can't be serialized in language independent format (set for 
example).

On Wednesday, December 13, 2017 at 3:36:59 PM UTC+2, Keith Brown wrote:
>
> is it possible to analyze python remote objects in go? we have several 
> people using it and i would like to view the calls 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.


[go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread Miki Tebeka
Do you mean

func C.GoBytes(unsafe.Pointer, C.int) []byte

 ?

On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:
>
> Hi all
>
> I'm trying to map a C structure to an equivalent go struct, but I bumped 
> into a problem with a pointer to byte that is actually an array of bytes.
>
> Here is the C struct:
>
> typedef struct _CERT_CONTEXT {
>   DWORD  dwCertEncodingType;
>   BYTE   *pbCertEncoded;
>   DWORD  cbCertEncoded;
>   PCERT_INFO pCertInfo;
>   HCERTSTORE hCertStore;
> } CERT_CONTEXT, *PCERT_CONTEXT;
>
>
> and this is my go struct:
>
> type CERT_CONTEXT struct {
> dwCertEncodingType uint32
> pbCertEncoded  uintptr
> cbCertEncoded  uint32
> pCertInfo  uintptr
> hCertStore uintptr
> }
>
> for my case I need only the first 3 fields and I do not have any problem 
> to get 1 and 3, but I can't remember how to translate the second field to a 
> slice of bytes.
> This is how I map the struct from an uintptr and print it to the console:
>
> certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
> fmt.Printf("%v\n", certctx)
> 
> >&{1 807520 674 833008 789360}
>
> Any advise is warmly welcome.
>
> Cheers,
> Sandro
>

-- 
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.