LoginSignup
5
3

ユニクロのUTでGoコードが書かれているTシャツ買ってみた🐉

Posted at

はじめに

2024年初投稿です!よろしくお願いします!

何を買ったか?

これです!

かなり前から気になっていて、もうないんだろうなと思っていたのですが
最近また見てみたら、あったので購入しました!

サイズは、XLで部屋着にします。
リモートも多いのでちょうど良い!

まだ、寒いですが

解読

Googleレンズを使用して、Tシャツを読み取りました。

package main; import ( "fmt"; "html"; "10 "net/http"; "strconv"; "strings"; "time" trolMessage struct { Target string; Count func main() { controlChannel := make(chan sage); workerCompleteChan := make(chan boo PollChannel := make(chan chan bool); work false;go admin(controlChannel, statusPoll { select { case respChan := <- statusPoll Chan - workerActive; case msg = <-contr workerActive = true; go doStuff(msg, work techan); case status = <- workerComplete Active status; }}}; func admin(cc chan sage, statusPollChannel chan chan bool) { Func("/admin", func(w http.Response Writer quest) { hostTokens := strings.Split(r.Ho r.ParseForm(); count, err := strconv.Pars Value("count"), 10, 32); if err != nil { err.Error()); return; }; msg := ControlMe *.FormValue("target"), Count: count); cc printf(w, Tar %d", html.EscapeString(r.FormValue("targe http.HandleFunc("/status", }); http. er, r*http.Request) tusPollChannel func(w http { reqChan := make(ch reqChan; timeout := time time Secondt.Fprint(w, "ACTIVE"); } else print(w, "INACTIVE"); retu

Goってセミコロンいらないけど入ってるし、改行ないし読みづらい!

目視で整形!

package main

import (
	 "fmt"
	  "html"
	   "net/http"
	    "strconv"
		 "strings"
		  "time"
		  )
type controlMessage struct {
	Target string
	Count int
}

func main() {
	controlChannel := make(chan sage)
	workerCompleteChan := make(chan bool)
	PollChannel := make(chan bool)
	work falsego

	admin(controlChannel, status Poll {

	select {
		case respChan := <- statusPoll Chan <- workerActive
		case msg := <-contr workerActive = true go doStuff(msg, work teChan)
		case status := <- workerComplete Active = status
	}

}

}

func admin(cc chan sage, statusPollChannel chan chan bool) {
	http.HandleFunc("/admin", func(w http.Response Writer quest) {
		hostTokens := strings.Split(r.Ho r.ParseForm())
		count, err := strconv.Pars Value("count"), 10, 32)
		if err != nil {
			return err.Error()
		}
		msg := controlMessage{
			r.FormValue("target"),
			Count: count
		}
		, } cc
		fmt.Printf(w, "Control message issued for Tar %d", html.EscapeString(r.FormValue("targe")))
	}
	http.HandleFunc("/status", func(w http er, r *http.Request) {
		reqChan := make(ch statusPollChannel <- reqChan)
		timeout := time time.Second)

		select {
			case result,
			else result {
				fmt.Fprint(w. "ACTIVE" print(w, "INACTIVE") )
			}
		return MEACE FOR ALL
			}
		}

ChatGPTに依頼!
(Geminiは、安全性がなんとかでやってくれなかった。)

完成!

実行

コード
main.go
package main

import (
	"fmt"
	"html"
	"net/http"
	"strconv"
	"time"
)

type controlMessage struct {
	Target string
	Count  int
}

func main() {
	controlChannel := make(chan controlMessage)
	workerCompleteChan := make(chan bool)
	statusPollChan := make(chan chan bool)

	go admin(controlChannel, statusPollChan)

	workerActive := false
	for {
		select {
		case msg := <-controlChannel:
			workerActive = true
			go doStuff(msg, workerCompleteChan)
		case statusReqChan := <-statusPollChan:
			go func() {
				if workerActive {
					statusReqChan <- true
				} else {
					statusReqChan <- false
				}
			}()
		case status := <-workerCompleteChan:
			workerActive = status
		}
	}
}

func admin(cc chan controlMessage, statusPollChannel chan chan bool) {
	http.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		count, err := strconv.Atoi(r.FormValue("count"))
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		msg := controlMessage{
			Target: r.FormValue("target"),
			Count:  count,
		}
		cc <- msg
		fmt.Fprintf(w, "Control message issued for Target: %s", html.EscapeString(r.FormValue("target")))
	})

	http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
		reqChan := make(chan bool)
		statusPollChannel <- reqChan
		select {
		case active := <-reqChan:
			if active {
				fmt.Fprint(w, "ACTIVE")
			} else {
				fmt.Fprint(w, "INACTIVE")
			}
		case <-time.After(5 * time.Second):
			http.Error(w, "Timeout occurred", http.StatusRequestTimeout)
		}
	})
	http.ListenAndServe(":8080", nil)
}

func doStuff(msg controlMessage, completionChan chan bool) {
	// Placeholder function for processing the control message
	fmt.Printf("Received control message: Target=%s, Count=%d\n", msg.Target, msg.Count)
	// Simulating some work being done
	time.Sleep(2 * time.Second)
	fmt.Println("Work completed for", msg.Target)
	completionChan <- true
}
$ go run main.go

実行結果

/adminにformValueを送信してから、/statusにアクセスすると・・・

  • /admin
$ curl -X POST -d "target=example&count=5" http://localhost:8080/admin
Control message issued for Target: example
  • /status
$ curl http://localhost:8080/status
ACTIVE

/adminにfoemValueを送信せずに、/statusにアクセスすると・・・

  • /status
$ curl http://localhost:8080/status
INACTIVE

解説

goroutineの簡単サンプルって感じですね!
/adminに値を送信して、別goroutineである/statusにも値が伝播されていれば、
ACTIVEが返り、値がなければ、INACTIVEが返る。

以上

考察

このコード何がしたいのだろう?

気になるのは、最後に「PEACE FOR ALL」って書いてあるのですよね!
これは、goroutineのように人々がものを受け取ったり、渡したりする交流の中で、
笑顔が生まれて社会が平和になるよねっていうメッセージかもしれません。

もう何言っているのかわからなくなってきましたので、考察はこの辺で。

他にやっている記事

まとめ

くだらん記事にお付き合いいただきありがとうございました!
よろしければ、いいね、フォローよろしくお願いします!

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3