From 9ef2c797644b195391dd24c82f7121e52345dc36 Mon Sep 17 00:00:00 2001 From: Polister <151947504+ZPolister@users.noreply.github.com> Date: Mon, 11 Aug 2025 00:38:52 +0800 Subject: [PATCH] =?UTF-8?q?docs(nil):=20=E7=BB=86=E5=8C=96=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=E7=9A=84=E6=AD=A7=E4=B9=89=EF=BC=8C=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81=E7=BC=A9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/essential/impl/err/3.nil.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/essential/impl/err/3.nil.md b/src/essential/impl/err/3.nil.md index f67fe7bbc..8ba8352b9 100644 --- a/src/essential/impl/err/3.nil.md +++ b/src/essential/impl/err/3.nil.md @@ -8,7 +8,7 @@ type A struct { b B c C - d D + d D } func (a A) Close() error { @@ -197,7 +197,7 @@ func main() { } ``` -当 map 为`nil`的时候,还可以对其进行访问 +当 map 为`nil`的时候,还可以对其进行访问,但`nil`的 map 是只读的,一旦尝试写入就会引发panic ```go func main() { @@ -205,6 +205,10 @@ func main() { i, ok := s[""] fmt.Println(i, ok) fmt.Println(len(s)) + + // 尝试写入时,会引发panic + s["a"] = 1 // panic: assignment to entry in nil map + } ```