Alex Howard | 29 Aug 15:36 2014
Picon

converting arbitrary map without reflection

Been using angular with Go with much success but I need to know is there any way to range over an arbitrary map?

I have my data map[string]*CustomData and want to send it to this function without writing custom function for each data type.

func MapToJSObject(m map[string]interface) {

for a, b := range m {
               j, err := json.Encode(b)
               // serialise as JS object[a] = b 
}

}

the golang.org people say that if i need to use reflection i'm doing something wrong, please help

--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Re: Generating files in build time

+nuts

consider asking your question on YAQS which is now the canonical
source of build help.

On Fri, Aug 29, 2014 at 11:33 AM, Jakub Czarnowicz
<jczarnowicz@...> wrote:
> Hello,
>
> My name is Jakub, I'm an intern in RTC SRE in Zurich, working on Goa at the
> moment. I need to get all packages names and descriptions (package comments)
> from a specified folder and use this data in runtime. The idea is to
> generate a .go file containing all the information during build time and use
> it in the final binary.
>
> The questions is: do you know how to access a proper folder in google3 given
> a perforce path (eg. "//foo/bar") using Go? And then how to generate a
> source file that can be used in future compilation?
>
> I'd be really grateful for any help and tips.

If you only want package documentation, you could use SrcFS (ie.
opening files in /google_src/ ), and then use the Go AST package to
parse them,

http://golang.org/pkg/go/ast/

you can also try to generate Go at build time, but that means

1. that you have to rebuild and deploy the binary every time you want
updated data.

2. you have to have support for all the OWNERs of the .go source you
want to slurp, as you'd need to change the BUILD files in those
directory.

If you want to do this at build time, you'd have to write a genrule
(see go/be), package it up as a build extension
(https://engdoc.corp.google.com/eng/howto/google3/build-extensions.xml?cl=head)
and use that in all the relevant BUILD files.

-- 
Han-Wen Nienhuys
Google Munich
hanwen@...

--

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

Robert Clausecker | 29 Aug 14:05 2014
Picon

Is it possible to write C functions that modify structs of types defined in Go code?

A couple of months ago, I wrote bindings to a C library that uses structs with non-standard alignment (i.e. packed structs). The structs look like this:

struct example {
    uint8_t bar
;
    size_t baz
;
};

I want to marshall these structs into equal Go structs like this:

type Example struct {
    bar uint8
    baz
uint
}

Now, since some fields have alignments that cannot be represented in Go (compare issue #7560), I have a hard time writing the marshalling code. Originally, I planned to write C functions that receive pointers to a C struct and the corresponding Go struct which do the marshalling and are called from Go with pointers allocated on the Go heap, but I haven't figured out yet how to write C functions whose arguments have types defined in Go code. I filed issue #8114 about this which hasn't received any attention yet.

I'd like to know how this kind of situation is handled by other wrappers. I wrote two Stackoverflow questions (1, 2) about the problems I'm having but I didn't receive any helpful answers. What can I do to solve the problems I'm having?

Your sincerely,
Robert Clausecker

--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Val | 29 Aug 13:25 2014
Picon

When to use shadowing

Hello
In Go it is legal, in an inner block, to declare a variable x with the same name as another variable x already declared in an outer block.
Inside the inner block, the new variable shadows the other one.
I understand that, if used inappropriately, it may be confusing and lead to bugs. I've read articles saying this shadowing feature was a drawback in the language.

My question is : are there situations where shadowing is useful and could not be avoided by a trivial renaming of the inner variable?

Thanks!

--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Fredrik Wallgren | 29 Aug 13:05 2014
Picon

Project structure and import cycle

Hello,

I'm building an app and have gotten into a problem with my project structure and I'm looking for a good solution. Hopefully someone here can point me in the right direction.

My problem is related to the import cycle, where a package depends on another package that is already imported.

My project structure uses a common (I think) structure, where the base library resides in the project directory and a binary resides in the cmd "sub-package". Then I have a scripting engine and a api package. The api package uses functionality from the base library and exposes it to the scripting engine. All good, but the problem occurs when something in the base library should be scriptable, so it needs access to the scripting package.

I've heard that one way to solve this is to not use "sub-packages" (I know that there is no such thing in go, everything is a package, but to set a name on it) until you really have to. But it feels nice and structured to split functionality over multiple files and then just use a package to group them. But I might have to reconsider that, unless someone here have a good suggestion.

To exemplify my structure I'll draw a crude diagram of the folder structure below.

project/
  - cmd
    - app
      - main.go
  - api
    - api1.go
    - ...
  - scripting
    - scripting1.go
    - ...
  - lib1.go
  - ...

Where api1.go imports project/, scripting1.go imports project/api/ and then lib1.go imports project/scripting/

Does anyone have a suggestion of another structure I can use and till keep the packages for related files. Or is it easiest to just put everything in the project/ package?

Regards,
Fredrik Wallgren

--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
ernstrohlicek | 29 Aug 11:05 2014
Picon

How to test that an interface has Method()?

Greetings,

I want to write a test, which checks that an interface has / contains a certain method. How can I do that?

I have included short sample code below.

The interface in question is Message.
The method in question is Headers().
An implementing class is NewsMessage.

Lets say I have this simplified code in main.go:

package main

func main
() {
   
return
}

type
NewsMessage struct {
}

func
(m *NewsMessage) Header() map[string]string {
   
return nil
}

type
Message interface {
   
Header() map[string]string
}





And this main_test.go:

package main

import (
   
"testing"
   
"github.com/stretchr/testify/assert"
)

func
TestWantNewsMessageToImplementMessage(t *testing.T) {
   
//var _ Message = (*NewsMessage)(nil)
   
assert.Implements(t, (*Message)(nil), new(NewsMessage), "NewsMessage must support interface Message")
}

func
TestMessageHasMethodHeader(t *testing.T) {
   
//stumbling here
   
//var thing Message
   
//var thing = Message{}
   
var thing Message = &NewsMessage()
   
if Aer, ok := thing.(interface{Header() map[string]string }); ok {
       
Aer.Header()
   
} else {
        t
.Error("Message does not implement method: Header() map[string]string")
   
}
}




So, I have a first test which checks that NewsMessage actually implements the Message interface. Thats OK.

But in the second test, I want to check that the Message interface itself actually has the method Header(), so that I can then modify the interface Message to include the required Header() method in order to make the test for the Header() method pass.

And this is where I am stumbling... as I see it, the second test is merely checking if NewsMessage implements the Header() method, but is there a way to directly check if an interface prescribes / requires a method? Thats what I would like to do, directly, instead of checking indirectly using some implementing type (in this case NewsMessage).

I found information on how to check that an object implements a method (using type assertion), but no info how to check whether an interface requires a method.

Is there a way? Please help.

--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Raph | 29 Aug 12:34 2014
Picon
Picon

[ANN] gostwriter: Simple virtual keyboard for linux

I'm working on a little project where I want to emulate keyboard inputs on a raspberry pi running linux. 

For that purpose I wrote gostwriter, a little golang API on top of /dev/uinput to inject keyboard events.

https://github.com/galaktor/gostwriter

It works for my purposes but certainly more could be done with it! Obviously it can't replace scripting but particularly when running a raspberry pi without keyboard attached it can be very useful to just fire in the desired key sequence/combination.
The code is quite fresh still and I can already think of many things I could do better.

The demo code shows how it's used, in case you're curious
https://github.com/galaktor/gostwriter/tree/master/demo

Constructive feedback is of course welcome, pull requests even more ;-) feel free to create issues with suggestions.

Thanks
_R


--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Vasiliy Tolstov | 29 Aug 12:30 2014
Picon

dns package for writing dns server

Hi. I found two good dns packages github.com/miekg/dns and
https://github.com/cznic/dns , but what package is maintained more
time and more stable?
I need to write something like https://github.com/abh/geodns/ (may be
not write from scratch, but modify some parts)

-- 
Vasiliy Tolstov,
e-mail: v.tolstov@...
jabber: vase@...

--

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

Alexander Kolesen | 29 Aug 10:47 2014
Picon

net/http: closed response body on redirect

Hello,

I have bumped into strange behaviour of net/http on reading response body if redirect happened.
Simplified code looks like this:


package main

import (
    "errors"
    "fmt"
    "io/ioutil"
    "net/http"
)

func redirectPolicyFunc(r *http.Request, rr []*http.Request) error {
    return errors.New("disable")
}

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Location", "/foo")
        w.WriteHeader(303)
        fmt.Fprintf(w, "Hello, World!")
    })
    go http.ListenAndServe(":8080", nil)

    httpclient := &http.Client{
        CheckRedirect: redirectPolicyFunc,
    }
    req, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
    resp, _ := httpclient.Do(req)

    body, err := ioutil.ReadAll(resp.Body)
    defer resp.Body.Close()

    if nil != err {
        fmt.Println("Error reading response body: ", err.Error())
    }
    fmt.Println("Body len: ", len(body))

}


Output:

Error reading response body:  http: read on closed response body
Body len:  0


Which means, that resp.Body was closed before.
I have digged into Golang sources and looks like resp.Body is closed here:

http://golang.org/src/pkg/net/http/client.go#L354

AFAIR, RFC2616 allows having response body on all 30* responses (except 304).
Could you please give me some insight, is it desired behaviour or bug?

--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Siddon Tang | 29 Aug 03:57 2014
Picon

[ANN]: A fast NoSQL LedisDB v0.3 release

Hi gophers:

Let me announce the v0.3 release for LedisDB.

From v0.2, the biggest improvement is to support transaction with LMDB or BoltDB. Now you can use `begin`, `commit`, `rollback` like common SQL database, but you must notice that, after you begin a transaction, LedisDB will block any other write operations until you commit or rollback. 

Example: 

ledis> begin
OK
ledis> set hello world
OK
ledis> get hello
"world"
ledis> rollback
OK
ledis> get hello
(nil)



LedisDB is a high performance NoSQL powered by Go,  it can be used in many scenarios like message push, statistics, cache, etc. 

Now we use LedisDB in production and need to improve it continuously, any advice is very welcome.

Thank you!

SiddonTang

--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
ChrisLu | 29 Aug 02:00 2014
Picon

organize multiple go projects in a company

It may have been discussed many times already, but I want to find the best way to organize multiple go projects in a company.

Currently I simply have one repo, and check everything into it, including external libraries from "go get". I will run a script to remove ".git", ".svn" folders that comes with "go get". I feel this is the simplest and best way to pin the version, handle dependency, etc, and easy for new go programers to start with.

It is working well. However, some other people want to use "godep" to avoid future possible versioning problems. This way each project can have its own godep configuration. 

However, how to share common code among internal projects in this case? For example, an internal developed logging library should be used by all internal projects. How to share the logging library?

Chris

--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Gmane