Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
GO 中 switch 表达式后加 true 的含义
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
北极狐  
View profile   Translate to Translated (View Original)
 More options Jul 18 2012, 9:11 pm
From: 北极狐 <bjhg...@gmail.com>
Date: Wed, 18 Jul 2012 18:11:37 -0700 (PDT)
Local: Wed, Jul 18 2012 9:11 pm
Subject: GO 中 switch 表达式后加 true 的含义

下面代码中
18            switch nr, er := f.Read(buf[:]); true {
这一行中,后面加入true的含义是什么??

17          for {
18            switch nr, er := f.Read(buf[:]); true {
19            case nr < 0:
20                fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", f.String(), er.String())
21                os.Exit(1)
22            case nr == 0:  // EOF
23                return
24            case nr > 0:
25                if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
26                    fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", f.String(), ew.String())
27                }
28            }
29        }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Specode  
View profile   Translate to Translated (View Original)
 More options Jul 18 2012, 10:45 pm
From: Specode <0x0000e...@gmail.com>
Date: Thu, 19 Jul 2012 10:45:39 +0800
Local: Wed, Jul 18 2012 10:45 pm
Subject: Re: [gocn:4811] GO 中 switch 表达式后加 true 的含义

http://tour.golang.org/#43
看这里,写true和不写是一样的

--
Freedom|Share|Focus|Persistent

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
北极狐  
View profile   Translate to Translated (View Original)
 More options Jul 18 2012, 11:39 pm
From: 北极狐 <bjhg...@gmail.com>
Date: Wed, 18 Jul 2012 20:39:58 -0700 (PDT)
Local: Wed, Jul 18 2012 11:39 pm
Subject: Re: [gocn:4811] GO 中 switch 表达式后加 true 的含义

恩,此时就类似于 if-else ,case中均是 条件表达式,但如果将 true 改为 false,又是什么意思,我测试了 false,发现也进入了
switch 里面,但结果就是乱七八糟的。。。


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leo Jay  
View profile   Translate to Translated (View Original)
 More options Jul 19 2012, 12:32 am
From: Leo Jay <python.leo...@gmail.com>
Date: Thu, 19 Jul 2012 12:32:25 +0800
Local: Thurs, Jul 19 2012 12:32 am
Subject: Re: [gocn:4814] GO 中 switch 表达式后加 true 的含义

On Thu, Jul 19, 2012 at 11:39 AM, 北极狐 <bjhg...@gmail.com> wrote:

> 恩,此时就类似于 if-else ,case中均是 条件表达式,但如果将 true 改为 false,又是什么意思,我测试了
> false,发现也进入了 switch 里面,但结果就是乱七八糟的。。。

switch就是if else chain。拿switch的表达式挨个跟case里的表达式比较,如果相等就执行case里的语句。
比方说这个switch
        switch a:=5; true {
        case a < 5:
                println("a < 5")
        case a > 5:
                println("a > 5")
        case a == 5:
                println("a == 5")
        }
等价于
        if a := 5; true == (a < 5) {
                println("a < 5")
        } else if true == (a > 5) {
                println("a > 5")
        } else if true == (a == 5) {
                println("a == 5")
        }

自然地,如果你把switch里的true写成false,那这个switch就等价于

        if a := 5; false == (a < 5) {
                println("a < 5")
        } else if false == (a > 5) {
                println("a > 5")
        } else if false == (a == 5) {
                println("a == 5")
        }

--
Best Regards,
Leo Jay


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
北极狐  
View profile   Translate to Translated (View Original)
 More options Jul 19 2012, 3:36 am
From: 北极狐 <bjhg...@gmail.com>
Date: Thu, 19 Jul 2012 00:36:03 -0700 (PDT)
Local: Thurs, Jul 19 2012 3:36 am
Subject: Re: [gocn:4814] GO 中 switch 表达式后加 true 的含义

thank you!
明白了,开始虽然知道这样是 if-else chain,但没有搞明白原理。


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »