trying to understand golang chan cause crash or do something elseAdding a func never called improves behavior?Is blocking on a channel send a bad synchronization paradigm and whywhat would cause code blocking in go?What's the difference between c:=make(chan int) and c:=make(chan int,1)?Convert chan to non chan in golangGolang: Why does increasing the size of a buffered channel eliminate output from my goroutines?Golang - Range over chan map by keyWhy can I not use func literal as custom func type despite matching parameters in golang?Multiple go routines consuming from a channel causing loss of datareturn early from range of a channel

Attempting to shape traffic for both IPv4 and IPv6 results in a conflict

Non-OR journals which regularly publish OR research

How to avoid the "need" to learn more before conducting research?

What is Poincare's "Fourth Geometry"?

Withdrew when Jimmy met up with Heath

Ex-contractor published company source code and secrets online

MinionPro is erroneous

Why should we care about syntactic proofs if we can show semantically that statements are true?

Why isn’t SHA-3 in wider use?

Visa National - No Exit Stamp From France on Return to the UK

Y2K... in 2019?

Is refreshing multiple times a test case for web applications?

Simple Stop watch which i want to extend

Infeasibility in mathematical optimization models

How to create all combinations from a nested list while preserving the structure using R?

Why did the RAAF procure the F/A-18 despite being purpose-built for carriers?

Can you castle with a "ghost" rook?

Are differences between uniformly distributed numbers uniformly distributed?

Three legged NOT gate? What is this symbol?

What is the maximum number of PC-controlled undead?

What game uses dice with sides powers of 2?

(11 of 11: Meta) What is Pyramid Cult's All-Time Favorite?

Acceptable to cut steak before searing?

Is it okay for a ticket seller to grab a tip in the USA?



trying to understand golang chan cause crash or do something else


Adding a func never called improves behavior?Is blocking on a channel send a bad synchronization paradigm and whywhat would cause code blocking in go?What's the difference between c:=make(chan int) and c:=make(chan int,1)?Convert chan to non chan in golangGolang: Why does increasing the size of a buffered channel eliminate output from my goroutines?Golang - Range over chan map by keyWhy can I not use func literal as custom func type despite matching parameters in golang?Multiple go routines consuming from a channel causing loss of datareturn early from range of a channel






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















The following is a example from https://golang.org/ref/mem:



var c = make(chan int)
var a string

func f()
a = "hello, world"
<-c

func main()
go f()
c <- 0
print(a)




is also guaranteed to print "hello, world". The write to a happens before the receive on c, which happens before the corresponding send on c completes, which happens before the print.



If the channel were buffered (e.g., c = make(chan int, 1)) then the program would not be guaranteed to print "hello, world". (It might print the empty string, crash, or do something else.)




I understand that It might print the empty string, but not for crash or do something else, when will crash happen? And when will it do something else ?










share|improve this question



















  • 7





    The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety

    – icza
    Mar 27 at 8:26


















1















The following is a example from https://golang.org/ref/mem:



var c = make(chan int)
var a string

func f()
a = "hello, world"
<-c

func main()
go f()
c <- 0
print(a)




is also guaranteed to print "hello, world". The write to a happens before the receive on c, which happens before the corresponding send on c completes, which happens before the print.



If the channel were buffered (e.g., c = make(chan int, 1)) then the program would not be guaranteed to print "hello, world". (It might print the empty string, crash, or do something else.)




I understand that It might print the empty string, but not for crash or do something else, when will crash happen? And when will it do something else ?










share|improve this question



















  • 7





    The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety

    – icza
    Mar 27 at 8:26














1












1








1








The following is a example from https://golang.org/ref/mem:



var c = make(chan int)
var a string

func f()
a = "hello, world"
<-c

func main()
go f()
c <- 0
print(a)




is also guaranteed to print "hello, world". The write to a happens before the receive on c, which happens before the corresponding send on c completes, which happens before the print.



If the channel were buffered (e.g., c = make(chan int, 1)) then the program would not be guaranteed to print "hello, world". (It might print the empty string, crash, or do something else.)




I understand that It might print the empty string, but not for crash or do something else, when will crash happen? And when will it do something else ?










share|improve this question














The following is a example from https://golang.org/ref/mem:



var c = make(chan int)
var a string

func f()
a = "hello, world"
<-c

func main()
go f()
c <- 0
print(a)




is also guaranteed to print "hello, world". The write to a happens before the receive on c, which happens before the corresponding send on c completes, which happens before the print.



If the channel were buffered (e.g., c = make(chan int, 1)) then the program would not be guaranteed to print "hello, world". (It might print the empty string, crash, or do something else.)




I understand that It might print the empty string, but not for crash or do something else, when will crash happen? And when will it do something else ?







go memory concurrency






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 8:23









Geln YangGeln Yang

5392 gold badges15 silver badges35 bronze badges




5392 gold badges15 silver badges35 bronze badges










  • 7





    The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety

    – icza
    Mar 27 at 8:26













  • 7





    The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety

    – icza
    Mar 27 at 8:26








7




7





The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety

– icza
Mar 27 at 8:26






The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety

– icza
Mar 27 at 8:26













1 Answer
1






active

oldest

votes


















3














A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.



The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.






share|improve this answer


























    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%2f55372640%2ftrying-to-understand-golang-chan-cause-crash-or-do-something-else%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









    3














    A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.



    The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.






    share|improve this answer































      3














      A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.



      The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.






      share|improve this answer





























        3












        3








        3







        A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.



        The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.






        share|improve this answer















        A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.



        The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 27 at 9:48

























        answered Mar 27 at 8:36









        Grzegorz ŻurGrzegorz Żur

        33.2k13 gold badges85 silver badges82 bronze badges




        33.2k13 gold badges85 silver badges82 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            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%2f55372640%2ftrying-to-understand-golang-chan-cause-crash-or-do-something-else%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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해