我试图从另一个内部类引用一个内部类。我都试过:
class Foo(object):
class A(object):
pass
class B(object):
other = A
Run Code Online (Sandbox Code Playgroud)
和
class Foo(object):
class A(object):
pass
class B(object):
other = Foo.A
Run Code Online (Sandbox Code Playgroud)
各自的结果:
Traceback (most recent call last):
File "python", line 1, in <module>
File "python", line 6, in Foo
File "python", line 7, in B
NameError: name 'A' is not defined
Run Code Online (Sandbox Code Playgroud)
和
Traceback (most recent call last):
File "python", line 1, in <module>
File "python", line 6, in Foo
File "python", line 7, in B
NameError: name 'Foo' is …Run Code Online (Sandbox Code Playgroud) 我正在尝试将签名的可恢复上传执行到 GCS。我们的前端在初始请求上遇到了 CORS 限制:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
响应头不显示 CORS 头:
alt-svc: quic=":443"; ma=2592000; v="44,43,39,35"
cache-control: private, max-age=0
content-length: 0
content-type: text/html; charset=UTF-8
date: Tue, 13 Nov 2018 20:28:32 GMT
expires: Tue, 13 Nov 2018 20:28:32 GMT
server: UploadServer
status: 200
x-guploader-uploadid: AEnB2Ups1tKbTbhPmsjrPXbIuIUyQt135AlSJ1n7-7XTwMrtQ2vUvn1WwpX3a_iusfmsXHaufdf5B3H2PzmDONs2wW7tKkLarYoxrVyWalhaX6FzGQPoRW0
调试时,我发送了一个 curl 请求来模仿我们前端的请求:
curl -H "Access-Control-Request-Headers: content-type,x-goog-resumable" \
-H "Access-Control-Request-Method: POST" \
-H "Origin: https://www.example.com" \
-X OPTIONS -I …
有没有一种方法可以将属性表示为只读,但允许在 POST 或 PUT(即创建或替换)操作期间写入该属性?
换句话说,在创建资源时,我希望该属性是可写的。但是一旦创建了资源,我想保持它不可变。属性可以是 POSTable/PUTable 但不是 PATCHable 吗?
例子:
# OK example.
/AwesomeResource POST
{"owner": "ownerID123"}
vs
# Bad Request example.
/AwesomeResource/456 PATCH
{"owner": "ownerID789"}
Run Code Online (Sandbox Code Playgroud) 我注意到在分离并重新连接屏幕会话后,我丢失了滚动历史记录。我已经做了一些搜索,但是每当我组合“屏幕”和“滚动”时,我就会被滚轮集成指令淹没。
无论如何,概要:
这是一个示例(另请参见https://play.golang.org/p/or7z4Xc8tN):
package main
import (
"encoding/json"
"fmt"
)
type A struct {
X string
Y int
}
type B struct {
A
Y string
}
func main() {
data := []byte(`{"X": "me", "Y": "hi"}`)
b := &B{}
json.Unmarshal(data, b)
fmt.Println(b)
fmt.Println(b.A)
b = &B{}
data = []byte(`{"X": "me", "Y": 123}`)
json.Unmarshal(data, b)
fmt.Println(b)
fmt.Println(b.A)
}
Run Code Online (Sandbox Code Playgroud)
哪些输出:
&{{me 0} hi}
{me 0}
&{{me 0} }
{me 0}
Run Code Online (Sandbox Code Playgroud)
有没有办法多态地将字段 Y 解组为 int 或 string?或者甚至因为 BY 被定义而完全解组为 AY ?
我知道有些人可能会建议使用诸如 之类的东西进行解组 …
我有一个经典的 Go nil 接口问题。
我试图断言 an interface{},我从 a 分配nil error回一个error接口。这句话很令人困惑,所以我有一个方便的例子: https: //play.golang.com/p/Qhv7197oIE_z
package main
import (
"fmt"
)
func preferredWay(i interface{}) error {
return i.(error)
}
func workAround(i interface{}) error {
if i == nil {
return nil
}
return i.(error)
}
func main() {
var nilErr error
fmt.Println(workAround(nilErr)) // Prints "<nil>" as expected.
fmt.Println(preferredWay(nilErr)) // Panics.
}
Run Code Online (Sandbox Code Playgroud)
输出:
<nil>
panic: interface conversion: interface is nil, not error
goroutine 1 [running]:
main.preferredWay(...)
/tmp/sandbox415300914/prog.go:8
main.main()
/tmp/sandbox415300914/prog.go:21 …Run Code Online (Sandbox Code Playgroud) go ×2
connexion ×1
cors ×1
downcast ×1
gnu-screen ×1
go-interface ×1
json ×1
linux ×1
nested ×1
oop ×1
openapi ×1
polymorphism ×1
python ×1
python-2.7 ×1
rest ×1
screenrc ×1
scroll ×1
terminal ×1