funcmakeslice(et *_type, len, capint)unsafe.Pointer { mem, overflow := math.MulUintptr(et.size, uintptr(cap)) if overflow || mem > maxAlloc || len < 0 || len > cap { // 先判断是否越界 mem, overflow := math.MulUintptr(et.size, uintptr(len)) if overflow || mem > maxAlloc || len < 0 { panicmakeslicelen() } panicmakeslicecap() }
// 内存分配 return mallocgc(mem, et, true) }
出于好奇看了一下 MaxUintptr 的源码:
// Copyright 2018 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.
package math
import"runtime/internal/sys"
const MaxUintptr = ^uintptr(0)
// MulUintptr returns a * b and whether the multiplication overflowed. // On supported platforms this is an intrinsic lowered by the compiler. funcMulUintptr(a, b uintptr)(uintptr, bool) { if a|b < 1<<(4*sys.PtrSize) || a == 0 { return a * b, false } overflow := b > MaxUintptr/a return a * b, overflow }
由源码可知,MulUintptr 接收两个参数,分别是要分配的类型大小 a 和要分配的数量 b,计算后返回要分配的内存空间以及是否溢出。
The tap command allows Homebrew to tap into another repository of formulae. Once you’ve done this you’ve expanded your options of installable software.
$ brew tap mongodb/brew
之后,运行如下命令安装 MongoDB:
$ brew install mongodb-community@4.2
启动
使用 brew 命令运行 MongoDB:
$ brew services start mongodb-community@4.2
建立连接
启动成功后,使用如下命令与 MongoDB 建立连接:
$ mongo
运行命令后发现连接失败,错误如下:
MongoDB shell version v4.2.2 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb 2020-01-31T19:24:23.752+0800 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused : connect@src/mongo/shell/mongo.js:341:17 @(connect):2:6 2020-01-31T19:24:23.768+0800 F - [main] exception: connect failed 2020-01-31T19:24:23.768+0800 E - [main] exiting with code 1
$ cat /usr/local/var/log/mongodb/mongo.log 2020-01-31T20:20:11.840+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' 2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] MongoDB starting : pid=44665 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=Jalan-JiangdeMacBook-Pro.local 2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] db version v4.2.2 2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf 2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] allocator: system 2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] modules: none 2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] build environment: 2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] distarch: x86_64 2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] target_arch: x86_64 2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] options: { config: "/usr/local/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, storage: { dbPath: "/usr/local/var/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/usr/local/var/log/mongodb/mongo.log" } } 2020-01-31T20:20:11.876+0800 I STORAGE [initandlisten] 2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] ** WARNING: Support for MMAPV1 storage engine has been deprecated and will be 2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] ** removed in version 4.2. Please plan to migrate to the wiredTiger 2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] ** storage engine. 2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/deprecated-mmapv1 2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] 2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] Detected data files in /usr/local/var/mongodb created by the 'mmapv1' storage engine, so setting the active storage engine to 'mmapv1'. 2020-01-31T20:20:11.878+0800 I STORAGE [initandlisten] exception in initAndListen: Location18656: Cannot start server with an unknown storage engine: mmapv1, terminating 2020-01-31T20:20:11.878+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets... 2020-01-31T20:20:11.878+0800 I NETWORK [initandlisten] removing socket file: /tmp/mongodb-27017.sock 2020-01-31T20:20:11.878+0800 I - [initandlisten] Stopping further Flow Control ticket acquisitions. 2020-01-31T20:20:11.879+0800 I CONTROL [initandlisten] now exiting 2020-01-31T20:20:11.879+0800 I CONTROL [initandlisten] shutting down with code:100
列出关键信息:WARNING: Support for MMAPV1 storage engine has been deprecated and will be removed in version 4.2. Please plan to migrate to the wiredTiger
为什么说字符只是整数的特殊用例呢?因为在 Go 中,用于表示字符的 byte 和 rune 类型都是整型的别名。在 Go 的源码中我们可以看到:
// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is // used, by convention, to distinguish byte values from 8-bit unsigned // integer values. typebyte = uint8
// rune is an alias for int32 and is equivalent to int32 in all ways. It is // used, by convention, to distinguish character values from integer values. typerune = int32