why the Singleton instances have different memory address?Difference between static class and singleton pattern?In Go, if type T2 is based on type T1, is there any sort of “inheritance” from T1 to T2?In Go, one type is coerced into another, can a method to determine the type of the receiver?Creating a singleton in PythonWhy can't I put the opening braces on the next line?difference of HttpClient singleton instance or notGo String after variable declarationWhy does putting a pointer in an interface in Go cause reflect to lose the name of the type?In c++, which reasons is true that the singleton(static instance) will be destroy once the program exitFunction executing itself without being called

Why can't I see bouncing of a switch on an oscilloscope?

Can a Warlock become Neutral Good?

Prove that NP is closed under karp reduction?

Fencing style for blades that can attack from a distance

How to test if a transaction is standard without spending real money?

What's the point of deactivating Num Lock on login screens?

What are these boxed doors outside store fronts in New York?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

Why doesn't H₄O²⁺ exist?

Why Is Death Allowed In the Matrix?

strToHex ( string to its hex representation as string)

Which models of the Boeing 737 are still in production?

What is the offset in a seaplane's hull?

How to say job offer in Mandarin/Cantonese?

What's the output of a record cartridge playing an out-of-speed record

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

Mathematical cryptic clues

Is it important to consider tone, melody, and musical form while writing a song?

Have astronauts in space suits ever taken selfies? If so, how?

Fully-Firstable Anagram Sets

Font hinting is lost in Chrome-like browsers (for some languages )

Can I make popcorn with any corn?

Service Entrance Breakers Rain Shield

Has the BBC provided arguments for saying Brexit being cancelled is unlikely?



why the Singleton instances have different memory address?


Difference between static class and singleton pattern?In Go, if type T2 is based on type T1, is there any sort of “inheritance” from T1 to T2?In Go, one type is coerced into another, can a method to determine the type of the receiver?Creating a singleton in PythonWhy can't I put the opening braces on the next line?difference of HttpClient singleton instance or notGo String after variable declarationWhy does putting a pointer in an interface in Go cause reflect to lose the name of the type?In c++, which reasons is true that the singleton(static instance) will be destroy once the program exitFunction executing itself without being called






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-3















when I create two instances of a Singleton, the memory address of the instances are
different which confused me. The code as below



import (
"fmt"
"sync"
)

type example3 struct
name string


var instance3 *example3
var once sync.Once

func GetInstance3() *example3
once.Do(func()
instance3 = new(example3)
)
return instance3


func main()
e1 := GetInstance3()
fmt.Printf("address of instance e1: %p n" ,&e1)

e2 := GetInstance3()
fmt.Printf("address of instance e2: %p n" ,&e2)



and the output as below



address of instance e1: 0xc000006028 
address of instance e2: 0xc000006038









share|improve this question

















  • 9





    You're printing the addresses of the variables e1 and e2, which, of course, are different. If you remove the &s, both should have the same value.

    – Andy Schweig
    Mar 8 at 4:35











  • finnally, I got it. The %p refer to the value of a point. So I do not need to use & before e1 and e2. Thank you!

    – sksun27
    Mar 8 at 6:08











  • sksun27, I don't quite get your wording. All "verbs"—those %something thingies govern how a matching value is formatted, and hence %p merely instructs a fmt.*f function to apply formatting useful for pointers (addresses). Your problem was taking an address of a variable already containing an address you were interested in.

    – kostix
    Mar 9 at 8:16

















-3















when I create two instances of a Singleton, the memory address of the instances are
different which confused me. The code as below



import (
"fmt"
"sync"
)

type example3 struct
name string


var instance3 *example3
var once sync.Once

func GetInstance3() *example3
once.Do(func()
instance3 = new(example3)
)
return instance3


func main()
e1 := GetInstance3()
fmt.Printf("address of instance e1: %p n" ,&e1)

e2 := GetInstance3()
fmt.Printf("address of instance e2: %p n" ,&e2)



and the output as below



address of instance e1: 0xc000006028 
address of instance e2: 0xc000006038









share|improve this question

















  • 9





    You're printing the addresses of the variables e1 and e2, which, of course, are different. If you remove the &s, both should have the same value.

    – Andy Schweig
    Mar 8 at 4:35











  • finnally, I got it. The %p refer to the value of a point. So I do not need to use & before e1 and e2. Thank you!

    – sksun27
    Mar 8 at 6:08











  • sksun27, I don't quite get your wording. All "verbs"—those %something thingies govern how a matching value is formatted, and hence %p merely instructs a fmt.*f function to apply formatting useful for pointers (addresses). Your problem was taking an address of a variable already containing an address you were interested in.

    – kostix
    Mar 9 at 8:16













-3












-3








-3








when I create two instances of a Singleton, the memory address of the instances are
different which confused me. The code as below



import (
"fmt"
"sync"
)

type example3 struct
name string


var instance3 *example3
var once sync.Once

func GetInstance3() *example3
once.Do(func()
instance3 = new(example3)
)
return instance3


func main()
e1 := GetInstance3()
fmt.Printf("address of instance e1: %p n" ,&e1)

e2 := GetInstance3()
fmt.Printf("address of instance e2: %p n" ,&e2)



and the output as below



address of instance e1: 0xc000006028 
address of instance e2: 0xc000006038









share|improve this question














when I create two instances of a Singleton, the memory address of the instances are
different which confused me. The code as below



import (
"fmt"
"sync"
)

type example3 struct
name string


var instance3 *example3
var once sync.Once

func GetInstance3() *example3
once.Do(func()
instance3 = new(example3)
)
return instance3


func main()
e1 := GetInstance3()
fmt.Printf("address of instance e1: %p n" ,&e1)

e2 := GetInstance3()
fmt.Printf("address of instance e2: %p n" ,&e2)



and the output as below



address of instance e1: 0xc000006028 
address of instance e2: 0xc000006038






go singleton






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 4:23









sksun27sksun27

41




41







  • 9





    You're printing the addresses of the variables e1 and e2, which, of course, are different. If you remove the &s, both should have the same value.

    – Andy Schweig
    Mar 8 at 4:35











  • finnally, I got it. The %p refer to the value of a point. So I do not need to use & before e1 and e2. Thank you!

    – sksun27
    Mar 8 at 6:08











  • sksun27, I don't quite get your wording. All "verbs"—those %something thingies govern how a matching value is formatted, and hence %p merely instructs a fmt.*f function to apply formatting useful for pointers (addresses). Your problem was taking an address of a variable already containing an address you were interested in.

    – kostix
    Mar 9 at 8:16












  • 9





    You're printing the addresses of the variables e1 and e2, which, of course, are different. If you remove the &s, both should have the same value.

    – Andy Schweig
    Mar 8 at 4:35











  • finnally, I got it. The %p refer to the value of a point. So I do not need to use & before e1 and e2. Thank you!

    – sksun27
    Mar 8 at 6:08











  • sksun27, I don't quite get your wording. All "verbs"—those %something thingies govern how a matching value is formatted, and hence %p merely instructs a fmt.*f function to apply formatting useful for pointers (addresses). Your problem was taking an address of a variable already containing an address you were interested in.

    – kostix
    Mar 9 at 8:16







9




9





You're printing the addresses of the variables e1 and e2, which, of course, are different. If you remove the &s, both should have the same value.

– Andy Schweig
Mar 8 at 4:35





You're printing the addresses of the variables e1 and e2, which, of course, are different. If you remove the &s, both should have the same value.

– Andy Schweig
Mar 8 at 4:35













finnally, I got it. The %p refer to the value of a point. So I do not need to use & before e1 and e2. Thank you!

– sksun27
Mar 8 at 6:08





finnally, I got it. The %p refer to the value of a point. So I do not need to use & before e1 and e2. Thank you!

– sksun27
Mar 8 at 6:08













sksun27, I don't quite get your wording. All "verbs"—those %something thingies govern how a matching value is formatted, and hence %p merely instructs a fmt.*f function to apply formatting useful for pointers (addresses). Your problem was taking an address of a variable already containing an address you were interested in.

– kostix
Mar 9 at 8:16





sksun27, I don't quite get your wording. All "verbs"—those %something thingies govern how a matching value is formatted, and hence %p merely instructs a fmt.*f function to apply formatting useful for pointers (addresses). Your problem was taking an address of a variable already containing an address you were interested in.

– kostix
Mar 9 at 8:16












0






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',
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056695%2fwhy-the-singleton-instances-have-different-memory-address%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056695%2fwhy-the-singleton-instances-have-different-memory-address%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







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко