Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a problem with iface's nil judgment? #266

Closed
ztorchan opened this issue Jun 12, 2024 · 2 comments
Closed

Is there a problem with iface's nil judgment? #266

ztorchan opened this issue Jun 12, 2024 · 2 comments

Comments

@ztorchan
Copy link

if c.GetCredential() == nil {
return nil, errors.New("AssumeRole require credential")
}

func (c *Client) GetCredential() CredentialIface {
return c.credential
}

type CredentialIface interface {
GetSecretId() string
GetToken() string
GetSecretKey() string
GetCredential() (string, string, string)
// needRefresh() bool
// refresh()
}

GetCredential() return a iface. Even if its value is nil, no error will be reported here when its type is not nil, which will cause panic in the following process.

@sesky4
Copy link
Collaborator

sesky4 commented Jun 12, 2024

hi ztorchan, can you elaborate more on when its type is not nil ? im not sure what that means.

@sesky4
Copy link
Collaborator

sesky4 commented Jan 6, 2025

oh, I see.

a interface will not be equal to nil even the value it contains is nil.
the following example will output

true true
false true
false false
package main

import (
	"io"
)

type Fake struct {
}

func (f *Fake) Write(b []byte) (int, error) {
	return 0, nil
}

func main() {
	var a io.Writer
	var b *Fake
	println(a == nil, b == nil)
	a = b
	println(a == nil, b == nil)
	b = &Fake{}
	println(a == nil, b == nil)
}

But i think its fine, since it will panic and show you the error stack.

@sesky4 sesky4 closed this as completed Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants