众所周知,在golang中,slice(切片)是我们最常使用到的一种数据结构,是一种可变长度的数组,本篇文章我们主要结合源码来介绍一下slice的底层实现,以及在使用slice时的一些注意事项。 Slice结构体 首先我们来看一段代码: package main import ( “fmt” “unsafe” ) func main() { var a int var b int8 var c int16 var d int32 var e int64 slice := make([]int, 0) slice = append(slice, 1) fmt.Printf(“int:%d\nint8:%d\nint16:%d\nint32:%d\nint64:%d\n”, unsafe.Sizeof(a), unsafe.Sizeof(b), unsafe.Sizeof(c), unsafe.Sizeof(d), unsafe.Sizeof(e)) fmt.Printf(“slice:%d”, unsafe.Sizeof(slice)) } 该程序输出golang中常用数据类型占多少byte,输出结果是:
PHP函数stream_set_timeout(Stream Functions)作用于读取流时的时间控制。fsockopen函数的timeout只管创建连接时的超时,对于连接后读取流时的超时,则需要用到 stream_set_timeout函数。由于国内的网络环境不是很稳定,尤其是连接国外的时候,不想程序出现Fatal error: Maximum execution time of 30 seconds exceeded in …的错误,该函数尤其有用。stream_set_timeout需配合stream_get_meta_data使用,如果没有timeout, stream_get_meta_data返回数组中time_out为空,反之为1,可根据此判断是否超时。另外由于PHP默认的Maximum execution time为30秒,这是一次执行周期的时间,为了不出现上述的Fatal error,还需要设置一个总的读取流的时间
$server=”www.yahoo.com”;
$port = 80;
$data=”GET / HTTP/1.0rn”;
$data.=”Connection: Closern”;
$data.=”User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)rnrn”;
$start_time = time();
$fp=fsockopen($server, $port, $errno, $errstr, 5);
if (!$fp) {
die(“Connect Timeout.n”);
} else {
stream_set_blocking($fp, True);
stream_set_timeout($fp, 3);
fputs($fp, “$data”);
while (!feof($fp)) {
$text .= fread($fp, 2000);
$diff = time() - $start_time;
if ($diff > 24) {
die(“Timeout!n”);
}
$status = stream_get_meta_data($fp);
if ($status[’timed_out’]) {
die(“Stream Timeout!n”);
}
}
}
fclose($fp);
Generally speaking, code generation is the process of producing code from some sort of abstract description. Although this is not a very expressive description, almost everyone has some sort of understanding of code generation. For example, in the cloud native ecosystem generating code from Protobuf or OpenAPI descriptors is quite common.