Define modules struct in main go

1,124 views
Skip to first unread message

RoboTamer

unread,
Sep 8, 2012, 3:55:30 AM9/8/12
to golan...@googlegroups.com

How do I use a struct that has been defined in the main application, in a module / library.
I am keep getting this error:
../gotamer/cfg/cfg.go:24: undefined: JsonCfg

// main.go
package main

import (
"gotamer/cfg"
"gotamer/e"
)

var Cfg JsonCfg

type JsonCfg struct{
Appl       string
Hostname   string
ListenAddr string
IpAddr     string
Debug      uint
}

func main() {
c, ok := cfg.Get(Cfg)
e.Fail(ok)
}


// cfg.go
package cfg

import (
"encoding/json"
"io/ioutil"
)

const file = "cfg.json" 
 
func Get(c *JsonCfg) (b []byte, ok error) {
b, ok = ioutil.ReadFile(file)
if ok != nil {
put() // Generates a template json file
return c, ok
}
ok = json.Unmarshal(b, &c)
if ok != nil {
println("Bad json File: ", ok)
}
return b, ok
}

Mike Rosset

unread,
Sep 8, 2012, 4:11:52 AM9/8/12
to RoboTamer, golan...@googlegroups.com
assuming you used cfg for you package name then 

var cfg cfg.JsonCfg

RoboTamer

unread,
Sep 8, 2012, 4:13:08 AM9/8/12
to golan...@googlegroups.com, RoboTamer

Tried that one already, doesn't work

Mike Rosset

unread,
Sep 8, 2012, 4:14:37 AM9/8/12
to RoboTamer, golan...@googlegroups.com
I replied to fast sorry. I dont think this will work. not in this order anyways.

since package cfg does not know what JsonCfg is.

DisposaBoy

unread,
Sep 8, 2012, 4:15:51 AM9/8/12
to golan...@googlegroups.com


On Saturday, September 8, 2012 8:55:30 AM UTC+1, RoboTamer wrote:

How do I use a struct that has been defined in the main application, in a module / library.
I am keep getting this error:
../gotamer/cfg/cfg.go:24: undefined: JsonCfg
 [..]

I assume by module/library you mean package? in that case, if JsonCfg is defined in another package then you need to import that package. In this case that other package is main which you can't import so your only option is to defined that type somewhere  that allows you to import it. From the sounds of it that JsonCfg should actually be in whatever package cfg.go is in then your main package can import that package to access the type.

Mike Rosset

unread,
Sep 8, 2012, 4:19:57 AM9/8/12
to RoboTamer, golan...@googlegroups.com
On Sat, Sep 8, 2012 at 12:55 AM, RoboTamer <grue...@gmail.com> wrote:

in this case I would use a function, and pass interface. basically your config struct.

see

RoboTamer

unread,
Sep 8, 2012, 4:21:43 AM9/8/12
to golan...@googlegroups.com

The thing is, I like the cfg.go package to be independent.
Meaning that is going to be a package on bitbucket or github
So the JsonCfg struct need to be defined outside of this package.

If I understand you right I need to create a 3rd package and just place the JsonCfg Struct in there?
That sucks, but I'll try it  

Mike Rosset

unread,
Sep 8, 2012, 4:23:58 AM9/8/12
to RoboTamer, golan...@googlegroups.com
no you do not need another package if you pass your struct as interface{}

RoboTamer

unread,
Sep 8, 2012, 4:40:36 AM9/8/12
to golan...@googlegroups.com, RoboTamer

Thank you, interface works!
interfaces to me are still magic, I can't wrap my head around them.
Do you know of a simple reading, that explains that to me.
Nothing I read so far makes total sense to me.   

Mike Rosset

unread,
Sep 8, 2012, 5:05:44 AM9/8/12
to RoboTamer, golan...@googlegroups.com

RoboTamer

unread,
Sep 8, 2012, 5:41:55 AM9/8/12
to golan...@googlegroups.com

Again thanks for your help.
Here is the result:
Enjoy, and let me know of any possible improvements or bugs

Mike Rosset

unread,
Sep 8, 2012, 6:10:45 AM9/8/12
to RoboTamer, golan...@googlegroups.com
Think your bitbucket repo is read only.

Mike Rosset

unread,
Sep 8, 2012, 6:11:14 AM9/8/12
to RoboTamer, golan...@googlegroups.com
sorry ment private

RoboTamer

unread,
Sep 8, 2012, 6:24:32 AM9/8/12
to golan...@googlegroups.com, RoboTamer
Sorry about that, it's the default at bitbucket 

Mike Rosset

unread,
Sep 8, 2012, 6:51:01 AM9/8/12
to RoboTamer, golan...@googlegroups.com
Looks good, only thing I might suggest is not using ok for error. ok generally is used for bool, and err for error.

RoboTamer

unread,
Sep 8, 2012, 2:45:18 PM9/8/12
to golan...@googlegroups.com, RoboTamer
On Saturday, September 8, 2012 3:51:07 AM UTC-7, Mike Rosset wrote:
Looks good, only thing I might suggest is not using ok for error. ok generally is used for bool, and err for error.

I didn't know about that convention, I updated the package, thanks 
Reply all
Reply to author
Forward
0 new messages