Help us understand the problem. What is going on with this article?

Go 言語でも (a==1 && a==2 && a==3) を true にしてみたい(クソリプ編)

More than 1 year has passed since last update.

元ネタ: javascript - Can (a ==1 && a== 2 && a==3) ever evaluate to true? - Stack Overflow

これに対する Qiita 記事のまとめが以下。

結論から言うと思いつきませんでした orz 理由は Go言語では演算子をオーバーライドできないし暗黙的な型変換もできないため。まともでないやり方なら何かあるかもしれないが思いつかなかった。

ただ Perl での解

use strict;
use warnings;

my $a = 1;
my @a = (1, 2);
sub a { 3 };

if ($a == 1 and @a == 2 and &a == 3) {
    print "True!\n";
}

というのを見かけたので,こういう記述が許容できるのなら何とかなりそうである。すなわち channel を使って

package main

import (
    "fmt"
)

func main() {
    a := make(chan int)
    go func() {
        for i := 1; ; i++ {
            a <- i
            i %= 3
        }
    }()

    if <-a == 1 && <-a == 2 && <-a == 3 {
        fmt.Println("True!")
    }
    if <-a == 1 && <-a == 2 && <-a == 3 {
        fmt.Println("True!")
    }
}

すればよい 1。お後がよろしいようで。


  1. 余談だが a <- i<- は channel への送信を表す送信構文(send statement)のトークンで for 構文内の <-a<- は channel からの受信を表す受信演算子(receive operator)である。演算子なので式(expression)に含めることができる。 

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした