Print contents of a channel in Go










-3















How can I print the contents of a channel in Go?



For example:



package main

import "fmt"

func main()
ok := make(chan int)
ok <- 1
x := <- ok
fmt.Println(x)



As I understand, ok is a channel which can store an integer value. So, how can I print its contents?



fmt.Println(ok) doesn't print the value stored inside the channel.



Thanks.










share|improve this question



















  • 1





    An unbuffered channel doesn't store anything by design, so ok doesn't contain any data to print.

    – JimB
    Nov 12 '18 at 17:32







  • 2





    Also note that "print contents of a channel" isn't really a thing. Your fmt.Println reads a value from the channel and prints it. Reading a value from a channel removes it from the channel. The Tour of Go should provide you a solid foundation on how channels work.

    – Adrian
    Nov 12 '18 at 17:35











  • There are no trivial solutions to print values in a channel without dequeuing them. So if you want to just read and print you may read print and write to another channel

    – Sarath Sadasivan Pillai
    Nov 12 '18 at 17:45












  • I updated the question. Could you take a look? I stored a value into the channel. Then retrieved the value from the channel and now I'm trying to print it.

    – Neon Flash
    Nov 12 '18 at 17:46











  • @NeonFlash: you did not store a value into the channel, because the channel cannot store a value. However even if you made it a buffered, there is no language method to read the stored value without receiving out of the channel.

    – JimB
    Nov 12 '18 at 17:56















-3















How can I print the contents of a channel in Go?



For example:



package main

import "fmt"

func main()
ok := make(chan int)
ok <- 1
x := <- ok
fmt.Println(x)



As I understand, ok is a channel which can store an integer value. So, how can I print its contents?



fmt.Println(ok) doesn't print the value stored inside the channel.



Thanks.










share|improve this question



















  • 1





    An unbuffered channel doesn't store anything by design, so ok doesn't contain any data to print.

    – JimB
    Nov 12 '18 at 17:32







  • 2





    Also note that "print contents of a channel" isn't really a thing. Your fmt.Println reads a value from the channel and prints it. Reading a value from a channel removes it from the channel. The Tour of Go should provide you a solid foundation on how channels work.

    – Adrian
    Nov 12 '18 at 17:35











  • There are no trivial solutions to print values in a channel without dequeuing them. So if you want to just read and print you may read print and write to another channel

    – Sarath Sadasivan Pillai
    Nov 12 '18 at 17:45












  • I updated the question. Could you take a look? I stored a value into the channel. Then retrieved the value from the channel and now I'm trying to print it.

    – Neon Flash
    Nov 12 '18 at 17:46











  • @NeonFlash: you did not store a value into the channel, because the channel cannot store a value. However even if you made it a buffered, there is no language method to read the stored value without receiving out of the channel.

    – JimB
    Nov 12 '18 at 17:56













-3












-3








-3








How can I print the contents of a channel in Go?



For example:



package main

import "fmt"

func main()
ok := make(chan int)
ok <- 1
x := <- ok
fmt.Println(x)



As I understand, ok is a channel which can store an integer value. So, how can I print its contents?



fmt.Println(ok) doesn't print the value stored inside the channel.



Thanks.










share|improve this question
















How can I print the contents of a channel in Go?



For example:



package main

import "fmt"

func main()
ok := make(chan int)
ok <- 1
x := <- ok
fmt.Println(x)



As I understand, ok is a channel which can store an integer value. So, how can I print its contents?



fmt.Println(ok) doesn't print the value stored inside the channel.



Thanks.







go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 17:57









cmaher

3,70311128




3,70311128










asked Nov 12 '18 at 17:30









Neon FlashNeon Flash

1,26783769




1,26783769







  • 1





    An unbuffered channel doesn't store anything by design, so ok doesn't contain any data to print.

    – JimB
    Nov 12 '18 at 17:32







  • 2





    Also note that "print contents of a channel" isn't really a thing. Your fmt.Println reads a value from the channel and prints it. Reading a value from a channel removes it from the channel. The Tour of Go should provide you a solid foundation on how channels work.

    – Adrian
    Nov 12 '18 at 17:35











  • There are no trivial solutions to print values in a channel without dequeuing them. So if you want to just read and print you may read print and write to another channel

    – Sarath Sadasivan Pillai
    Nov 12 '18 at 17:45












  • I updated the question. Could you take a look? I stored a value into the channel. Then retrieved the value from the channel and now I'm trying to print it.

    – Neon Flash
    Nov 12 '18 at 17:46











  • @NeonFlash: you did not store a value into the channel, because the channel cannot store a value. However even if you made it a buffered, there is no language method to read the stored value without receiving out of the channel.

    – JimB
    Nov 12 '18 at 17:56












  • 1





    An unbuffered channel doesn't store anything by design, so ok doesn't contain any data to print.

    – JimB
    Nov 12 '18 at 17:32







  • 2





    Also note that "print contents of a channel" isn't really a thing. Your fmt.Println reads a value from the channel and prints it. Reading a value from a channel removes it from the channel. The Tour of Go should provide you a solid foundation on how channels work.

    – Adrian
    Nov 12 '18 at 17:35











  • There are no trivial solutions to print values in a channel without dequeuing them. So if you want to just read and print you may read print and write to another channel

    – Sarath Sadasivan Pillai
    Nov 12 '18 at 17:45












  • I updated the question. Could you take a look? I stored a value into the channel. Then retrieved the value from the channel and now I'm trying to print it.

    – Neon Flash
    Nov 12 '18 at 17:46











  • @NeonFlash: you did not store a value into the channel, because the channel cannot store a value. However even if you made it a buffered, there is no language method to read the stored value without receiving out of the channel.

    – JimB
    Nov 12 '18 at 17:56







1




1





An unbuffered channel doesn't store anything by design, so ok doesn't contain any data to print.

– JimB
Nov 12 '18 at 17:32






An unbuffered channel doesn't store anything by design, so ok doesn't contain any data to print.

– JimB
Nov 12 '18 at 17:32





2




2





Also note that "print contents of a channel" isn't really a thing. Your fmt.Println reads a value from the channel and prints it. Reading a value from a channel removes it from the channel. The Tour of Go should provide you a solid foundation on how channels work.

– Adrian
Nov 12 '18 at 17:35





Also note that "print contents of a channel" isn't really a thing. Your fmt.Println reads a value from the channel and prints it. Reading a value from a channel removes it from the channel. The Tour of Go should provide you a solid foundation on how channels work.

– Adrian
Nov 12 '18 at 17:35













There are no trivial solutions to print values in a channel without dequeuing them. So if you want to just read and print you may read print and write to another channel

– Sarath Sadasivan Pillai
Nov 12 '18 at 17:45






There are no trivial solutions to print values in a channel without dequeuing them. So if you want to just read and print you may read print and write to another channel

– Sarath Sadasivan Pillai
Nov 12 '18 at 17:45














I updated the question. Could you take a look? I stored a value into the channel. Then retrieved the value from the channel and now I'm trying to print it.

– Neon Flash
Nov 12 '18 at 17:46





I updated the question. Could you take a look? I stored a value into the channel. Then retrieved the value from the channel and now I'm trying to print it.

– Neon Flash
Nov 12 '18 at 17:46













@NeonFlash: you did not store a value into the channel, because the channel cannot store a value. However even if you made it a buffered, there is no language method to read the stored value without receiving out of the channel.

– JimB
Nov 12 '18 at 17:56





@NeonFlash: you did not store a value into the channel, because the channel cannot store a value. However even if you made it a buffered, there is no language method to read the stored value without receiving out of the channel.

– JimB
Nov 12 '18 at 17:56












2 Answers
2






active

oldest

votes


















2














Channels make(chan int) has implicit size zero ( ref: https://golang.org/ref/spec#Making_slices_maps_and_channels)



A channel of size zero is unbuffered. A channel of specified size make(chan int, n) is buffered. See http://golang.org/ref/spec#Send_statements for a discussion on buffered vs. unbuffered channels. The example at http://play.golang.org/p/VZAiN1V8-P illustrates the difference.



Here, channel <-ok or ok <- will be blocked until someone processes it (concurrently). So, change ok := make(chan int) to ok := make(chan int,1)



package main

import "fmt"

func main()
ok := make(chan int, 1)
ok <- 1
x := <- ok
fmt.Println(x)



Or concurrently proccess it



package main

import "fmt"

func main()
ok := make(chan int)
go func()
ok <- 1
()
x := <- ok
fmt.Println(x)






share|improve this answer






























    1














    Here you are trying to write to an unbuffered channel since there are no go routines trying to read from the channel it will reach a deadlock situation



    Here is what you can do



    package main

    import (
    "fmt"
    "time"
    )

    func main()
    ok := make(chan int)

    go func()
    for x := range ok
    fmt.Println(x)

    ()
    ok <- 1
    ok <- 3
    ok <- 2
    ok <- 5
    ok <- 3
    ok <- 9
    time.Sleep(1)



    you may find the link to play ground here






    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%2f53267239%2fprint-contents-of-a-channel-in-go%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Channels make(chan int) has implicit size zero ( ref: https://golang.org/ref/spec#Making_slices_maps_and_channels)



      A channel of size zero is unbuffered. A channel of specified size make(chan int, n) is buffered. See http://golang.org/ref/spec#Send_statements for a discussion on buffered vs. unbuffered channels. The example at http://play.golang.org/p/VZAiN1V8-P illustrates the difference.



      Here, channel <-ok or ok <- will be blocked until someone processes it (concurrently). So, change ok := make(chan int) to ok := make(chan int,1)



      package main

      import "fmt"

      func main()
      ok := make(chan int, 1)
      ok <- 1
      x := <- ok
      fmt.Println(x)



      Or concurrently proccess it



      package main

      import "fmt"

      func main()
      ok := make(chan int)
      go func()
      ok <- 1
      ()
      x := <- ok
      fmt.Println(x)






      share|improve this answer



























        2














        Channels make(chan int) has implicit size zero ( ref: https://golang.org/ref/spec#Making_slices_maps_and_channels)



        A channel of size zero is unbuffered. A channel of specified size make(chan int, n) is buffered. See http://golang.org/ref/spec#Send_statements for a discussion on buffered vs. unbuffered channels. The example at http://play.golang.org/p/VZAiN1V8-P illustrates the difference.



        Here, channel <-ok or ok <- will be blocked until someone processes it (concurrently). So, change ok := make(chan int) to ok := make(chan int,1)



        package main

        import "fmt"

        func main()
        ok := make(chan int, 1)
        ok <- 1
        x := <- ok
        fmt.Println(x)



        Or concurrently proccess it



        package main

        import "fmt"

        func main()
        ok := make(chan int)
        go func()
        ok <- 1
        ()
        x := <- ok
        fmt.Println(x)






        share|improve this answer

























          2












          2








          2







          Channels make(chan int) has implicit size zero ( ref: https://golang.org/ref/spec#Making_slices_maps_and_channels)



          A channel of size zero is unbuffered. A channel of specified size make(chan int, n) is buffered. See http://golang.org/ref/spec#Send_statements for a discussion on buffered vs. unbuffered channels. The example at http://play.golang.org/p/VZAiN1V8-P illustrates the difference.



          Here, channel <-ok or ok <- will be blocked until someone processes it (concurrently). So, change ok := make(chan int) to ok := make(chan int,1)



          package main

          import "fmt"

          func main()
          ok := make(chan int, 1)
          ok <- 1
          x := <- ok
          fmt.Println(x)



          Or concurrently proccess it



          package main

          import "fmt"

          func main()
          ok := make(chan int)
          go func()
          ok <- 1
          ()
          x := <- ok
          fmt.Println(x)






          share|improve this answer













          Channels make(chan int) has implicit size zero ( ref: https://golang.org/ref/spec#Making_slices_maps_and_channels)



          A channel of size zero is unbuffered. A channel of specified size make(chan int, n) is buffered. See http://golang.org/ref/spec#Send_statements for a discussion on buffered vs. unbuffered channels. The example at http://play.golang.org/p/VZAiN1V8-P illustrates the difference.



          Here, channel <-ok or ok <- will be blocked until someone processes it (concurrently). So, change ok := make(chan int) to ok := make(chan int,1)



          package main

          import "fmt"

          func main()
          ok := make(chan int, 1)
          ok <- 1
          x := <- ok
          fmt.Println(x)



          Or concurrently proccess it



          package main

          import "fmt"

          func main()
          ok := make(chan int)
          go func()
          ok <- 1
          ()
          x := <- ok
          fmt.Println(x)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 17:48









          nightfury1204nightfury1204

          1,58148




          1,58148























              1














              Here you are trying to write to an unbuffered channel since there are no go routines trying to read from the channel it will reach a deadlock situation



              Here is what you can do



              package main

              import (
              "fmt"
              "time"
              )

              func main()
              ok := make(chan int)

              go func()
              for x := range ok
              fmt.Println(x)

              ()
              ok <- 1
              ok <- 3
              ok <- 2
              ok <- 5
              ok <- 3
              ok <- 9
              time.Sleep(1)



              you may find the link to play ground here






              share|improve this answer



























                1














                Here you are trying to write to an unbuffered channel since there are no go routines trying to read from the channel it will reach a deadlock situation



                Here is what you can do



                package main

                import (
                "fmt"
                "time"
                )

                func main()
                ok := make(chan int)

                go func()
                for x := range ok
                fmt.Println(x)

                ()
                ok <- 1
                ok <- 3
                ok <- 2
                ok <- 5
                ok <- 3
                ok <- 9
                time.Sleep(1)



                you may find the link to play ground here






                share|improve this answer

























                  1












                  1








                  1







                  Here you are trying to write to an unbuffered channel since there are no go routines trying to read from the channel it will reach a deadlock situation



                  Here is what you can do



                  package main

                  import (
                  "fmt"
                  "time"
                  )

                  func main()
                  ok := make(chan int)

                  go func()
                  for x := range ok
                  fmt.Println(x)

                  ()
                  ok <- 1
                  ok <- 3
                  ok <- 2
                  ok <- 5
                  ok <- 3
                  ok <- 9
                  time.Sleep(1)



                  you may find the link to play ground here






                  share|improve this answer













                  Here you are trying to write to an unbuffered channel since there are no go routines trying to read from the channel it will reach a deadlock situation



                  Here is what you can do



                  package main

                  import (
                  "fmt"
                  "time"
                  )

                  func main()
                  ok := make(chan int)

                  go func()
                  for x := range ok
                  fmt.Println(x)

                  ()
                  ok <- 1
                  ok <- 3
                  ok <- 2
                  ok <- 5
                  ok <- 3
                  ok <- 9
                  time.Sleep(1)



                  you may find the link to play ground here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 17:52









                  Sarath Sadasivan PillaiSarath Sadasivan Pillai

                  3,9431227




                  3,9431227



























                      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%2f53267239%2fprint-contents-of-a-channel-in-go%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

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo