Pointers vs Values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm a bit wondering why the following code does work:
var serverStartedTime time.Time // Holds the time since the server is started.
type ServerInformation struct
Uptime ServerUptimeInformation `json:"uptime"`
type ServerUptimeInformation struct
Hours int64 `json:"hours"`
Minutes int64 `json:"minutes"`
Seconds int64 `json:"seconds"`
NanoSeconds int64 `json:"nanoSeconds"`
func main()
serverStartedTime = time.Now()
http.HandleFunc("/api/v1/health", getHealthHandler)
log.Fatal(http.ListenAndServe(":8000", nil))
func handler(writer http.ResponseWriter, request *http.Request)
fmt.Fprintf(writer, "URL.Path = %qn", request.URL.Path)
func getHealthHandler(writer http.ResponseWriter, request *http.Request)
serverUptime := time.Now().Sub(serverStartedTime)
hours := int64(serverUptime.Hours())
minutes := int64(serverUptime.Minutes()) - (hours * 60)
seconds := int64(serverUptime.Seconds()) - (hours * 60) - (minutes * 60)
nanoSeconds := int64(serverUptime.Nanoseconds()) - (hours * 60) - (minutes * 60) - (seconds * 1000000000)
serverInformation := ServerInformation
ServerUptimeInformation
hours, minutes, seconds, nanoSeconds,
,
returnJSON(writer, serverInformation)
func returnJSON(writer http.ResponseWriter, data ...interface)
dataJSON, marshalError := json.Marshal(data)
if marshalError != nil
writer.WriteHeader(http.StatusInternalServerError)
else
writer.WriteHeader(http.StatusOK)
writer.Header().Set("Content-Type", "application/json")
writer.Write(dataJSON)
By default Go copies parameters that are provided to methods.
So, the HTTP handler for '/api/v1/health' does take a writer and we pass it to the returnJSON
method.
So, this method does receives a copy on which it writes.
How does it come that in my browser, I do see the response?
I didn't expected that since the writer is being copied.
go
add a comment |
I'm a bit wondering why the following code does work:
var serverStartedTime time.Time // Holds the time since the server is started.
type ServerInformation struct
Uptime ServerUptimeInformation `json:"uptime"`
type ServerUptimeInformation struct
Hours int64 `json:"hours"`
Minutes int64 `json:"minutes"`
Seconds int64 `json:"seconds"`
NanoSeconds int64 `json:"nanoSeconds"`
func main()
serverStartedTime = time.Now()
http.HandleFunc("/api/v1/health", getHealthHandler)
log.Fatal(http.ListenAndServe(":8000", nil))
func handler(writer http.ResponseWriter, request *http.Request)
fmt.Fprintf(writer, "URL.Path = %qn", request.URL.Path)
func getHealthHandler(writer http.ResponseWriter, request *http.Request)
serverUptime := time.Now().Sub(serverStartedTime)
hours := int64(serverUptime.Hours())
minutes := int64(serverUptime.Minutes()) - (hours * 60)
seconds := int64(serverUptime.Seconds()) - (hours * 60) - (minutes * 60)
nanoSeconds := int64(serverUptime.Nanoseconds()) - (hours * 60) - (minutes * 60) - (seconds * 1000000000)
serverInformation := ServerInformation
ServerUptimeInformation
hours, minutes, seconds, nanoSeconds,
,
returnJSON(writer, serverInformation)
func returnJSON(writer http.ResponseWriter, data ...interface)
dataJSON, marshalError := json.Marshal(data)
if marshalError != nil
writer.WriteHeader(http.StatusInternalServerError)
else
writer.WriteHeader(http.StatusOK)
writer.Header().Set("Content-Type", "application/json")
writer.Write(dataJSON)
By default Go copies parameters that are provided to methods.
So, the HTTP handler for '/api/v1/health' does take a writer and we pass it to the returnJSON
method.
So, this method does receives a copy on which it writes.
How does it come that in my browser, I do see the response?
I didn't expected that since the writer is being copied.
go
2
I don't understand what you're asking. Your title mentions pointers and values. Your body has a large block of code, and then your question is "Why do I see a response?" Please narrow your question down drastically, and include only relevant code. What is your specific question about pointers and values?
– Flimzy
Nov 16 '18 at 12:40
The question here is why I do see a value since the writer is copied over to a new method.
– Complexity
Nov 16 '18 at 12:47
add a comment |
I'm a bit wondering why the following code does work:
var serverStartedTime time.Time // Holds the time since the server is started.
type ServerInformation struct
Uptime ServerUptimeInformation `json:"uptime"`
type ServerUptimeInformation struct
Hours int64 `json:"hours"`
Minutes int64 `json:"minutes"`
Seconds int64 `json:"seconds"`
NanoSeconds int64 `json:"nanoSeconds"`
func main()
serverStartedTime = time.Now()
http.HandleFunc("/api/v1/health", getHealthHandler)
log.Fatal(http.ListenAndServe(":8000", nil))
func handler(writer http.ResponseWriter, request *http.Request)
fmt.Fprintf(writer, "URL.Path = %qn", request.URL.Path)
func getHealthHandler(writer http.ResponseWriter, request *http.Request)
serverUptime := time.Now().Sub(serverStartedTime)
hours := int64(serverUptime.Hours())
minutes := int64(serverUptime.Minutes()) - (hours * 60)
seconds := int64(serverUptime.Seconds()) - (hours * 60) - (minutes * 60)
nanoSeconds := int64(serverUptime.Nanoseconds()) - (hours * 60) - (minutes * 60) - (seconds * 1000000000)
serverInformation := ServerInformation
ServerUptimeInformation
hours, minutes, seconds, nanoSeconds,
,
returnJSON(writer, serverInformation)
func returnJSON(writer http.ResponseWriter, data ...interface)
dataJSON, marshalError := json.Marshal(data)
if marshalError != nil
writer.WriteHeader(http.StatusInternalServerError)
else
writer.WriteHeader(http.StatusOK)
writer.Header().Set("Content-Type", "application/json")
writer.Write(dataJSON)
By default Go copies parameters that are provided to methods.
So, the HTTP handler for '/api/v1/health' does take a writer and we pass it to the returnJSON
method.
So, this method does receives a copy on which it writes.
How does it come that in my browser, I do see the response?
I didn't expected that since the writer is being copied.
go
I'm a bit wondering why the following code does work:
var serverStartedTime time.Time // Holds the time since the server is started.
type ServerInformation struct
Uptime ServerUptimeInformation `json:"uptime"`
type ServerUptimeInformation struct
Hours int64 `json:"hours"`
Minutes int64 `json:"minutes"`
Seconds int64 `json:"seconds"`
NanoSeconds int64 `json:"nanoSeconds"`
func main()
serverStartedTime = time.Now()
http.HandleFunc("/api/v1/health", getHealthHandler)
log.Fatal(http.ListenAndServe(":8000", nil))
func handler(writer http.ResponseWriter, request *http.Request)
fmt.Fprintf(writer, "URL.Path = %qn", request.URL.Path)
func getHealthHandler(writer http.ResponseWriter, request *http.Request)
serverUptime := time.Now().Sub(serverStartedTime)
hours := int64(serverUptime.Hours())
minutes := int64(serverUptime.Minutes()) - (hours * 60)
seconds := int64(serverUptime.Seconds()) - (hours * 60) - (minutes * 60)
nanoSeconds := int64(serverUptime.Nanoseconds()) - (hours * 60) - (minutes * 60) - (seconds * 1000000000)
serverInformation := ServerInformation
ServerUptimeInformation
hours, minutes, seconds, nanoSeconds,
,
returnJSON(writer, serverInformation)
func returnJSON(writer http.ResponseWriter, data ...interface)
dataJSON, marshalError := json.Marshal(data)
if marshalError != nil
writer.WriteHeader(http.StatusInternalServerError)
else
writer.WriteHeader(http.StatusOK)
writer.Header().Set("Content-Type", "application/json")
writer.Write(dataJSON)
By default Go copies parameters that are provided to methods.
So, the HTTP handler for '/api/v1/health' does take a writer and we pass it to the returnJSON
method.
So, this method does receives a copy on which it writes.
How does it come that in my browser, I do see the response?
I didn't expected that since the writer is being copied.
go
go
edited Nov 16 '18 at 12:39
Flimzy
40.6k1367101
40.6k1367101
asked Nov 16 '18 at 12:37
ComplexityComplexity
3,24522157
3,24522157
2
I don't understand what you're asking. Your title mentions pointers and values. Your body has a large block of code, and then your question is "Why do I see a response?" Please narrow your question down drastically, and include only relevant code. What is your specific question about pointers and values?
– Flimzy
Nov 16 '18 at 12:40
The question here is why I do see a value since the writer is copied over to a new method.
– Complexity
Nov 16 '18 at 12:47
add a comment |
2
I don't understand what you're asking. Your title mentions pointers and values. Your body has a large block of code, and then your question is "Why do I see a response?" Please narrow your question down drastically, and include only relevant code. What is your specific question about pointers and values?
– Flimzy
Nov 16 '18 at 12:40
The question here is why I do see a value since the writer is copied over to a new method.
– Complexity
Nov 16 '18 at 12:47
2
2
I don't understand what you're asking. Your title mentions pointers and values. Your body has a large block of code, and then your question is "Why do I see a response?" Please narrow your question down drastically, and include only relevant code. What is your specific question about pointers and values?
– Flimzy
Nov 16 '18 at 12:40
I don't understand what you're asking. Your title mentions pointers and values. Your body has a large block of code, and then your question is "Why do I see a response?" Please narrow your question down drastically, and include only relevant code. What is your specific question about pointers and values?
– Flimzy
Nov 16 '18 at 12:40
The question here is why I do see a value since the writer is copied over to a new method.
– Complexity
Nov 16 '18 at 12:47
The question here is why I do see a value since the writer is copied over to a new method.
– Complexity
Nov 16 '18 at 12:47
add a comment |
1 Answer
1
active
oldest
votes
You think that ResponseWriter is a struct, but it is an interface.
Every time you send writer http.ResponseWriter
into your method you send pointer to struct that implements that interface.
Execute this line to see actual type:
fmt.Printf("%Tn", writer)
That makes sense. Thanks for the clarification.
– Complexity
Nov 16 '18 at 13:06
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f53338067%2fpointers-vs-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You think that ResponseWriter is a struct, but it is an interface.
Every time you send writer http.ResponseWriter
into your method you send pointer to struct that implements that interface.
Execute this line to see actual type:
fmt.Printf("%Tn", writer)
That makes sense. Thanks for the clarification.
– Complexity
Nov 16 '18 at 13:06
add a comment |
You think that ResponseWriter is a struct, but it is an interface.
Every time you send writer http.ResponseWriter
into your method you send pointer to struct that implements that interface.
Execute this line to see actual type:
fmt.Printf("%Tn", writer)
That makes sense. Thanks for the clarification.
– Complexity
Nov 16 '18 at 13:06
add a comment |
You think that ResponseWriter is a struct, but it is an interface.
Every time you send writer http.ResponseWriter
into your method you send pointer to struct that implements that interface.
Execute this line to see actual type:
fmt.Printf("%Tn", writer)
You think that ResponseWriter is a struct, but it is an interface.
Every time you send writer http.ResponseWriter
into your method you send pointer to struct that implements that interface.
Execute this line to see actual type:
fmt.Printf("%Tn", writer)
answered Nov 16 '18 at 12:55
Dmitry HarnitskiDmitry Harnitski
3,86311834
3,86311834
That makes sense. Thanks for the clarification.
– Complexity
Nov 16 '18 at 13:06
add a comment |
That makes sense. Thanks for the clarification.
– Complexity
Nov 16 '18 at 13:06
That makes sense. Thanks for the clarification.
– Complexity
Nov 16 '18 at 13:06
That makes sense. Thanks for the clarification.
– Complexity
Nov 16 '18 at 13:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53338067%2fpointers-vs-values%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
2
I don't understand what you're asking. Your title mentions pointers and values. Your body has a large block of code, and then your question is "Why do I see a response?" Please narrow your question down drastically, and include only relevant code. What is your specific question about pointers and values?
– Flimzy
Nov 16 '18 at 12:40
The question here is why I do see a value since the writer is copied over to a new method.
– Complexity
Nov 16 '18 at 12:47