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










share|improve this question



















  • 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






  • 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










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




    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















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










share|improve this question



















  • 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






  • 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










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




    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













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










share|improve this question















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







go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




    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






  • 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






  • 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




    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




    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






  • 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






  • 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


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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