Skip to content

Commit

Permalink
modify interface values
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingbx committed Nov 7, 2023
1 parent c12e618 commit bf8f09b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 217-Interface-Values/interface-values-1.gop
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ println r == nil

var r2 io.Reader = r // r2 has non-zero interface value. Its dynamic type is *bytes.Reader and its dynamic value is nil. So the result of r2 == nil is false

println r2 == nil
if r2 != nil {
slice := []byte{}
r2.Read(slice) // panic: runtime error: invalid memory address or nil pointer dereference
}

// The interface variable w has zero value. So both its dynamic type and its dynamic value are nil. In this case, the result of if w == nil is true.
// The interface variable r2 has non-zero value. Its dynamic type is *bytes.Reader and its dynamic value is nil. So the result of r2 == nil is false.

0 comments on commit bf8f09b

Please sign in to comment.