anonymous struct and empty struct

后端 未结 5 2003
猫巷女王i
猫巷女王i 2020-12-02 09:31

http://play.golang.org/p/vhaKi5uVmm

package main

import \"fmt\"

var battle = make(chan string)

func warrior(name string, done chan struct{}) {
    select          


        
5条回答
  •  难免孤独
    2020-12-02 10:10

    Good questions,

    The whole point of the struct channel in this scenario is simply to signal the completion that something useful has happened. The channel type doesn't really matter, he could have used an int or a bool to accomplish the same effect. What's important is that his code is executing in a synchronized fashion where he's doing the necessary bookkeeping to signal and move on at key points.

    I agree the syntax of struct{}{} looks odd at first because in this example he is declaring a struct and creating it in-line hence the second set of brackets.

    If you had a pre-existing object like:

    type Book struct{
    
    }
    

    You could create it like so: b := Book{}, you only need one set of brackets because the Book struct has already been declared.

提交回复
热议问题