我想组成另一种类型的类型,但用假的替换其中一个字段(这是一个接口值).我得到的问题是正在使用底层字段,所以我似乎无法覆盖该字段.
我在这里演示了这个问题:https://play.golang.org/p/lHGnyjzIS-Y
package main
import (
"fmt"
)
type Printer interface {
Print()
}
type PrinterService struct {}
func (ps PrinterService) Print() { fmt.Println("PrinterService") }
type Service struct {
Client PrinterService
}
func (s Service) PrintViaMethod() { s.Client.Print() }
type FakeService struct {
Service
Client Printer
}
type SomeOtherService struct {}
func (sos SomeOtherService) Print() { fmt.Println("SomeOtherService") }
func main() {
s := FakeService{Client: SomeOtherService{}}
s.PrintViaMethod()
}
Run Code Online (Sandbox Code Playgroud)
为什么要打印"PrinterService"?我想要它打印"SomeOtherService".
谢谢.
我正在阅读Effective Go 文档中关于for语句的部分,并遇到了这个例子:
for pos, char := range "??\x80?" {
fmt.Printf("Character %#U, at position: %d\n", char, pos)
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Character U+65E5 '?', at position: 0
Character U+672C '?', at position: 3
Character U+FFFD '?', at position: 6
Character U+8A9E '?', at position: 7
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么位置是 0、3、6 和 7。这告诉我第一个和第二个字符长 3 个字节,“替换符文”(U + FFFD)长 1 个字节,我接受和理解。但是,我认为rune是int32类型,因此每个是 4 个字节,而不是 3 个。
为什么某个范围内的位置与每个值应该消耗的内存总量不同?
我有一个 Rails 应用程序,除其他外,它还为第三方服务的 API 调用提供了一个简单的包装器。我想设置一条以 开头的路线/api,但添加到其末尾的任何内容都将被视为字符串变量。例如,如果客户请求:
/api/apps/guid/details
Run Code Online (Sandbox Code Playgroud)
...然后我想调用index控制器的操作并使其可以使用该ApiController字符串。/apps/guid/details
我已经阅读了有关控制器和路由的文档,但一切似乎都假设这/apps/guid/details将是我的应用程序中的资源,而实际上我并不关心/api.
我如何设置一条路线来执行此操作?