funcTestFibonacciList(t *testing.T) { //var a int = 1 // 定义变量 //var b int = 1 var ( a int = 1 b int = 1 ) // 这样也可以定义变量 // a := 1 直接赋值 // b := 1 t.Log(a) for i := 0; i < 5; i++ { t.Log(" ", b) tmp := a a = b b = tmp + a } }
变量及常量
变量
赋值可以进行自动类型推断
在一个赋值语句中可以对多个变量进行同时赋值
1 2 3 4 5 6 7 8 9
funcTestExchange(t *testing.T) { a := 1 b := 2 //tmp := a //a = b //b = tmp a, b = b, a // 变量交换 t.Log(a, b) }
funcTestSwitchMultiCase(t *testing.T) { for i := 0; i < 5; i++ { switch i { case0, 2: t.Log("even") case1, 3: t.Log("odd") default: t.Log("it is not 0-3") } } }
funcTestSwitchCaseCondition(t *testing.T) { for i := 0; i < 5; i++ { switch { case i%2 == 0: t.Log("even") case i%2 == 1: t.Log("odd") default: t.Log("it is not 0-3") } } }
$ go run hello.go gaobinzhan Hello, World! 参数0:/var/folders/qr/9vkwk7xn5rzbtnmykx7sxyv00000gn/T/go-build024626496/b001/exe/hello 参数1:gaobinzhan exit status 100
<?php publicfunctionkillTask($tid) { if (!isset($this->taskMap[$tid])) { returnfalse; } unset($this->taskMap[$tid]); // This is a bit ugly and could be optimized so it does not have to walk the queue, // but assuming that killing tasks is rather rare I won't bother with it now foreach ($this->taskQueue as$i => $task) { if ($task->getTaskId() === $tid) { unset($this->taskQueue[$i]); break; } } echo"任务 $tid 被杀死\n"; returntrue; }
public function __construct($host, $port, $type) { $this->socket = stream_socket_server("{$type}://{$host}:{$port}"); stream_set_blocking($this->socket,0); // Set non blocking $this->socketList[(int)$this->socket] = $this->socket; return $this->socket; }
private function accept() { while (true) { $write = $except = []; $read = $this->socketList; stream_select($read,$write,$except,60); foreach ($read as $socket) $socket === $this->socket ? $this->createSocket() : $this->receive($socket); } }
private function createSocket(){ //Establish a connection with the client $client = stream_socket_accept($this->socket); (!empty($client && is_callable($this->onConnect))) && call_user_func_array($this->onConnect, [$this->socket, $client]); $this->socketList[(int)$client] = $client; }
public function __construct($host, $port, $type) { $this->server = new Worker($host, $port, $type); $this->server->on('connect',[$this,"onConnect"]); $this->server->on('receive',[$this,"onReceive"]); $this->server->on('close',[$this,"onClose"]);
$this->server->start(); }
public function onConnect($server,$fd){ echo 'onConnect '.$fd.PHP_EOL; }
public function onReceive($server,$fd,$data){ echo 'onReceive '.$fd.PHP_EOL; $this->server->send($fd,'this is server !!!'); }
public function onClose($fd){ echo 'onClose '.$fd.PHP_EOL; } } new Server('0.0.0.0','9501','tcp');
FROM alpine RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ && apk add gcc g++ libc-dev wget vim openssl-dev make linux-headers \ && rm -rf /var/cache/apk/*
RUN wget -O /usr/src/redis/redis-4.0.11.tar.gz "http://download.redis.io/releases/redis-4.0.11.tar.gz" \ && tar -xzf /usr/src/redis/redis-4.0.11.tar.gz -C /usr/src/redis \ && rm -rf /usr/src/redis/redis-4.0.11.tar.tgz
RUN cd /usr/src/redis/redis-4.0.11 && make && make PREFIX=/usr/local/redis install \ && ln -s /usr/local/redis/bin/* /usr/local/bin/ && rm -rf /usr/src/redis/redis-4.0.11