Import alias of file but supply pointer receiver to it
up vote
-3
down vote
favorite
Im trying to write an api there is a config file which has database connection details and is of type Config This gets attached to a function in my router as a pointer receiver the problem is that i need to chain the file alias to the function in the router first... what workss
My file structure:
./api.go
./transaction/txn.go
api.go
package api
import (
"../config"
txn "./transaction"
"github.com/go-chi/chi"
)
type Config struct
*config.Config
func New(configuration *config.Config) *Config
return &Configconfiguration
func (config *Config) Routes() *chi.Mux
router := chi.NewRouter()
router.Post("/eth", txn.DoEth)
return router
transaction/txn.go
package api
import (
"../../config"
"github.com/go-chi/render"
"net/http"
)
type Config struct
*config.Config
func (c *Config) DoEth(w http.ResponseWriter, r *http.Request)
defaultReturn := "stuff"
render.JSON(w, r, defaultReturn)
the problematic line is router.Post("/eth", txn.DoEth) without the import it would work like this router.Post("/eth", config.DoEth) but with the import i need it to work with router.Post("/eth", txn.config.DoEth) or router.Post("/eth", config.txn.DoEth) I just need the pointer receiver of the DoEth() function to work. Sorry if im not explaining this correctly, relatively new to golang
|
show 3 more comments
up vote
-3
down vote
favorite
Im trying to write an api there is a config file which has database connection details and is of type Config This gets attached to a function in my router as a pointer receiver the problem is that i need to chain the file alias to the function in the router first... what workss
My file structure:
./api.go
./transaction/txn.go
api.go
package api
import (
"../config"
txn "./transaction"
"github.com/go-chi/chi"
)
type Config struct
*config.Config
func New(configuration *config.Config) *Config
return &Configconfiguration
func (config *Config) Routes() *chi.Mux
router := chi.NewRouter()
router.Post("/eth", txn.DoEth)
return router
transaction/txn.go
package api
import (
"../../config"
"github.com/go-chi/render"
"net/http"
)
type Config struct
*config.Config
func (c *Config) DoEth(w http.ResponseWriter, r *http.Request)
defaultReturn := "stuff"
render.JSON(w, r, defaultReturn)
the problematic line is router.Post("/eth", txn.DoEth) without the import it would work like this router.Post("/eth", config.DoEth) but with the import i need it to work with router.Post("/eth", txn.config.DoEth) or router.Post("/eth", config.txn.DoEth) I just need the pointer receiver of the DoEth() function to work. Sorry if im not explaining this correctly, relatively new to golang
1
Shouldn't txn.go bepackage transactionnotpackage api? Also this is generally a rather unusual design - if both packages need to use the sameConfigtype, that type should be in its own package, and these two packages should have functions that take aConfiginstead of each declaring different methods on it.
– Adrian
Nov 9 at 21:13
1
Why embed*config.Configin aapi.Configstruct when it's the only value? You could have use*config.Configdirectly.
– Pie 'Oh' Pah
Nov 9 at 21:15
@Adrian I was thinking that too but im a little unsure what the rules are for go design/architecture. I could technically put all the functions for this into 1 file but i want to break them out to make it easier to manage and read for myself. api.go is the main file which i want to include 2 sub directories into which contain functions relating to a specific part of the api's functions. Could you give an example for me to look at how i should be implementing this
– Kravitz
Nov 9 at 21:17
1
Also thistxn.config.DoEthdoesn't look like something that would work, consideringconfigis lower-case and un-exported.
– Pie 'Oh' Pah
Nov 9 at 21:18
1
You have to stop thinking in terms of files. The compilation in unit in Go is a package, not a file. One directory represents exactly one package and vice versa.
– Peter
Nov 10 at 4:03
|
show 3 more comments
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
Im trying to write an api there is a config file which has database connection details and is of type Config This gets attached to a function in my router as a pointer receiver the problem is that i need to chain the file alias to the function in the router first... what workss
My file structure:
./api.go
./transaction/txn.go
api.go
package api
import (
"../config"
txn "./transaction"
"github.com/go-chi/chi"
)
type Config struct
*config.Config
func New(configuration *config.Config) *Config
return &Configconfiguration
func (config *Config) Routes() *chi.Mux
router := chi.NewRouter()
router.Post("/eth", txn.DoEth)
return router
transaction/txn.go
package api
import (
"../../config"
"github.com/go-chi/render"
"net/http"
)
type Config struct
*config.Config
func (c *Config) DoEth(w http.ResponseWriter, r *http.Request)
defaultReturn := "stuff"
render.JSON(w, r, defaultReturn)
the problematic line is router.Post("/eth", txn.DoEth) without the import it would work like this router.Post("/eth", config.DoEth) but with the import i need it to work with router.Post("/eth", txn.config.DoEth) or router.Post("/eth", config.txn.DoEth) I just need the pointer receiver of the DoEth() function to work. Sorry if im not explaining this correctly, relatively new to golang
Im trying to write an api there is a config file which has database connection details and is of type Config This gets attached to a function in my router as a pointer receiver the problem is that i need to chain the file alias to the function in the router first... what workss
My file structure:
./api.go
./transaction/txn.go
api.go
package api
import (
"../config"
txn "./transaction"
"github.com/go-chi/chi"
)
type Config struct
*config.Config
func New(configuration *config.Config) *Config
return &Configconfiguration
func (config *Config) Routes() *chi.Mux
router := chi.NewRouter()
router.Post("/eth", txn.DoEth)
return router
transaction/txn.go
package api
import (
"../../config"
"github.com/go-chi/render"
"net/http"
)
type Config struct
*config.Config
func (c *Config) DoEth(w http.ResponseWriter, r *http.Request)
defaultReturn := "stuff"
render.JSON(w, r, defaultReturn)
the problematic line is router.Post("/eth", txn.DoEth) without the import it would work like this router.Post("/eth", config.DoEth) but with the import i need it to work with router.Post("/eth", txn.config.DoEth) or router.Post("/eth", config.txn.DoEth) I just need the pointer receiver of the DoEth() function to work. Sorry if im not explaining this correctly, relatively new to golang
edited Nov 17 at 1:07
dlsniper
2,85611325
2,85611325
asked Nov 9 at 20:58
Kravitz
5434825
5434825
1
Shouldn't txn.go bepackage transactionnotpackage api? Also this is generally a rather unusual design - if both packages need to use the sameConfigtype, that type should be in its own package, and these two packages should have functions that take aConfiginstead of each declaring different methods on it.
– Adrian
Nov 9 at 21:13
1
Why embed*config.Configin aapi.Configstruct when it's the only value? You could have use*config.Configdirectly.
– Pie 'Oh' Pah
Nov 9 at 21:15
@Adrian I was thinking that too but im a little unsure what the rules are for go design/architecture. I could technically put all the functions for this into 1 file but i want to break them out to make it easier to manage and read for myself. api.go is the main file which i want to include 2 sub directories into which contain functions relating to a specific part of the api's functions. Could you give an example for me to look at how i should be implementing this
– Kravitz
Nov 9 at 21:17
1
Also thistxn.config.DoEthdoesn't look like something that would work, consideringconfigis lower-case and un-exported.
– Pie 'Oh' Pah
Nov 9 at 21:18
1
You have to stop thinking in terms of files. The compilation in unit in Go is a package, not a file. One directory represents exactly one package and vice versa.
– Peter
Nov 10 at 4:03
|
show 3 more comments
1
Shouldn't txn.go bepackage transactionnotpackage api? Also this is generally a rather unusual design - if both packages need to use the sameConfigtype, that type should be in its own package, and these two packages should have functions that take aConfiginstead of each declaring different methods on it.
– Adrian
Nov 9 at 21:13
1
Why embed*config.Configin aapi.Configstruct when it's the only value? You could have use*config.Configdirectly.
– Pie 'Oh' Pah
Nov 9 at 21:15
@Adrian I was thinking that too but im a little unsure what the rules are for go design/architecture. I could technically put all the functions for this into 1 file but i want to break them out to make it easier to manage and read for myself. api.go is the main file which i want to include 2 sub directories into which contain functions relating to a specific part of the api's functions. Could you give an example for me to look at how i should be implementing this
– Kravitz
Nov 9 at 21:17
1
Also thistxn.config.DoEthdoesn't look like something that would work, consideringconfigis lower-case and un-exported.
– Pie 'Oh' Pah
Nov 9 at 21:18
1
You have to stop thinking in terms of files. The compilation in unit in Go is a package, not a file. One directory represents exactly one package and vice versa.
– Peter
Nov 10 at 4:03
1
1
Shouldn't txn.go be
package transaction not package api? Also this is generally a rather unusual design - if both packages need to use the same Config type, that type should be in its own package, and these two packages should have functions that take a Config instead of each declaring different methods on it.– Adrian
Nov 9 at 21:13
Shouldn't txn.go be
package transaction not package api? Also this is generally a rather unusual design - if both packages need to use the same Config type, that type should be in its own package, and these two packages should have functions that take a Config instead of each declaring different methods on it.– Adrian
Nov 9 at 21:13
1
1
Why embed
*config.Config in a api.Config struct when it's the only value? You could have use *config.Config directly.– Pie 'Oh' Pah
Nov 9 at 21:15
Why embed
*config.Config in a api.Config struct when it's the only value? You could have use *config.Config directly.– Pie 'Oh' Pah
Nov 9 at 21:15
@Adrian I was thinking that too but im a little unsure what the rules are for go design/architecture. I could technically put all the functions for this into 1 file but i want to break them out to make it easier to manage and read for myself. api.go is the main file which i want to include 2 sub directories into which contain functions relating to a specific part of the api's functions. Could you give an example for me to look at how i should be implementing this
– Kravitz
Nov 9 at 21:17
@Adrian I was thinking that too but im a little unsure what the rules are for go design/architecture. I could technically put all the functions for this into 1 file but i want to break them out to make it easier to manage and read for myself. api.go is the main file which i want to include 2 sub directories into which contain functions relating to a specific part of the api's functions. Could you give an example for me to look at how i should be implementing this
– Kravitz
Nov 9 at 21:17
1
1
Also this
txn.config.DoEth doesn't look like something that would work, considering config is lower-case and un-exported.– Pie 'Oh' Pah
Nov 9 at 21:18
Also this
txn.config.DoEth doesn't look like something that would work, considering config is lower-case and un-exported.– Pie 'Oh' Pah
Nov 9 at 21:18
1
1
You have to stop thinking in terms of files. The compilation in unit in Go is a package, not a file. One directory represents exactly one package and vice versa.
– Peter
Nov 10 at 4:03
You have to stop thinking in terms of files. The compilation in unit in Go is a package, not a file. One directory represents exactly one package and vice versa.
– Peter
Nov 10 at 4:03
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233195%2fimport-alias-of-file-but-supply-pointer-receiver-to-it%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Shouldn't txn.go be
package transactionnotpackage api? Also this is generally a rather unusual design - if both packages need to use the sameConfigtype, that type should be in its own package, and these two packages should have functions that take aConfiginstead of each declaring different methods on it.– Adrian
Nov 9 at 21:13
1
Why embed
*config.Configin aapi.Configstruct when it's the only value? You could have use*config.Configdirectly.– Pie 'Oh' Pah
Nov 9 at 21:15
@Adrian I was thinking that too but im a little unsure what the rules are for go design/architecture. I could technically put all the functions for this into 1 file but i want to break them out to make it easier to manage and read for myself. api.go is the main file which i want to include 2 sub directories into which contain functions relating to a specific part of the api's functions. Could you give an example for me to look at how i should be implementing this
– Kravitz
Nov 9 at 21:17
1
Also this
txn.config.DoEthdoesn't look like something that would work, consideringconfigis lower-case and un-exported.– Pie 'Oh' Pah
Nov 9 at 21:18
1
You have to stop thinking in terms of files. The compilation in unit in Go is a package, not a file. One directory represents exactly one package and vice versa.
– Peter
Nov 10 at 4:03