跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Go 编程语言] go 关于函数返回 error 的一个疑问

发表于
type A struct {
	a int
}

func f() (*A, error) {
	// do something
	// if err != nil {
	// 	return nil, err
	// }
	return nil, nil
}

func TestX(t *testing.T) {
	a, err := f()
	if err != nil {
		t.Log(err)
	} else {
		t.Log(a.a) // 这里 a 为 nil ,所以 a.a 非法访问,崩溃
	}
}

go 关于错误处理,似乎默认都是大家约定,如果 err != nil ,另外一个值是正常的值,但是理论上来说,即使 err = nil ,另外一个值也不一定是合理的值。感觉下面的写法似乎更好一点。

func TestX(t *testing.T) {
	a, err := f()
	if a == nil {
		t.Log(err)
	} else {
		t.Log(a.a)
	}
}

Featured Replies

没有可显示的帖子

创建帐户或登录来提出意见

Account

导航

搜索

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.