为什么这个代码不会编译?
package main
const a = 1.000001
const base = 0
const b = a+base
func main() {
f(b)
}
func f(int) {}
Run Code Online (Sandbox Code Playgroud)
$ go run a.go
# command-line-arguments
./a.go:4: constant 1 truncated to integer
Run Code Online (Sandbox Code Playgroud)
这是说1被截断了?或者1不能被截断?它在谈论哪一个?
有人回答上面的代码没有编译因为b是float64.但是为什么这会编译:
package main
import "fmt"
const a = 1.000001
const b = a-0.000001
func main() {
fmt.Printf("%T %v\n",a,a)
fmt.Printf("%T %v\n",b,b)
f(b)
}
func f(int) {}
Run Code Online (Sandbox Code Playgroud)
$ go run a.go
float64 1.000001
float64 1
Run Code Online (Sandbox Code Playgroud)
?b是一个float64在这里,但它可以传递给f.
假设我有一个类型type T int,我想定义一个逻辑来操作这种类型.
我应该使用什么抽象和什么时候?
在该类型上定义方法:
func (T t) someLogic() {
// ...
}
Run Code Online (Sandbox Code Playgroud)定义一个功能:
func somelogic(T t) {
// ...
}
Run Code Online (Sandbox Code Playgroud)我正在做一个通过传递的url参数下载文件的过程.下载正在正常完成,但我不能做的是打印下载完成百分比的摘要.( 每一秒 )
我已经建立了一个模拟类型的这个摘要,但它没有下载任何东西,它只是为了表明我想要它.
我试图将io.copy带入我的源代码中,这样我就可以在复制完成时将其更改为ant print,但它失败了.
有人能帮我吗?谢谢
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
// "time"
)
func downloadFromUrl(url string) {
tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
fmt.Println("Downloading", url, "to", fileName)
//create file
output, err := os.Create(fileName)
if err != nil {
fmt.Println("Error while creating", fileName, "-", err)
return
}
fmt.Println("Creating", fileName)
defer output.Close()
//get url
response, err := http.Get(url)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return
}
defer response.Body.Close()
//copy and …Run Code Online (Sandbox Code Playgroud) 基本上,我想要实现的是获取目录的内容,os.ReadDir()然后将结果编码为json.
直接做的事情json.Marshal()没有例外,但给了我一个空洞的结果.
所以我尝试了这个:
func (f *os.FileInfo) MarshalerJSON() ([]byte, error) {
return f.Name(), nil
}
Run Code Online (Sandbox Code Playgroud)
然后Go告诉我这os.FileInfo()是一个接口,不能以这种方式扩展.
最好的方法是什么?
我想在Go中调用一个函数,在方法值上使用reflect.Value.Call,并将nil作为参数传递.请参阅下面的代码以获取说明.
我已经尝试在输入数组中使用reflect.ValueOf(nil)和reflect.Value{},但是第一次恐慌是因为nil没有值; 当我把它传递给Call时,第二次恐慌,因为它是一个Zero reflect.Value.
请注意,正如代码所示,当然可以将nil传递给没有反射的函数,包括当该参数是接收器时.问题是:是否可以使用reflect.Value.Call调用func,将其中一个参数传递为nil?
您可以在http://play.golang.org/p/x9NXMDHWdM上构建并运行以下代码
package main
import "reflect"
type Thing struct{}
var (
thingPointer = &Thing{}
typ = reflect.TypeOf(thingPointer)
)
func (t *Thing) DoSomething() {
if t == nil {
println("t was nil")
} else {
println("t was not nil")
}
}
func normalInvokation() {
thingPointer.DoSomething()
// prints "t was not nil"
t := thingPointer
t = nil
t.DoSomething()
// prints "t was nil"
}
func reflectCallNonNil() {
m, _ …Run Code Online (Sandbox Code Playgroud) a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4 }
fmt.Println(a)
Run Code Online (Sandbox Code Playgroud)
结果是[5 4 3 2 1 0].怎么样?
a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4 ,12,11,10}
fmt.Println(a)
Run Code Online (Sandbox Code Playgroud)
结果是
prog.go:8: duplicate index in array literal: 2
prog.go:8: duplicate index in array literal: 3
prog.go:8: duplicate index in array literal: 4
[process exited with non-zero status]
Run Code Online (Sandbox Code Playgroud)
谁能解释这两个结果?
Template.ParseGlob("*.html") //fetches all html files from current directory.
Template.ParseGlob("**/*.html") //Seems to only fetch at one level depth
Run Code Online (Sandbox Code Playgroud)
我不是在寻找"Walk"解决方案.只是想知道这是否可行.我不太明白这种期望的"模式".如果我可以得到关于ParseGlob使用的模式的解释,那也很棒.
我正在寻找一种RangeTable从runeGo中获取unicode category()的方法.例如,角色a映射到Ll类别.该unicode软件包指定了所有类别(http://golang.org/pkg/unicode/#pkg-variables),但我没有看到任何方法从给定的类别中查找类别rune.我是否需要RangeTable从rune使用适当的偏移量手动构建?
我是新手,试图找到一个字符串中的字符串索引,但我想传入起始索引.
我知道有strings.Index和strings.LastIndex,但他们只找到第一个和最后一个.有没有我可以使用的功能,我可以在哪里指定起始索引?像我的例子中的最后一行.
例:
s := "go gopher, go"
fmt.Println(strings.Index(s, "go")) // Position 0
fmt.Println(strings.LastIndex(s, "go")) // Postion 11
fmt.Println(strings.Index(s, "go", 1)) // Position 3 - Start looking for "go" begining at index 1
Run Code Online (Sandbox Code Playgroud) 由于某种原因mgo,即使我已经设置了omitempty选项,也将空结构插入到db as null值中.
package main
import (
"fmt"
"encoding/json"
)
type A struct {
A bool
}
type B struct {
X int `json:"x,omitempty" bson:"x,omitempty"`
SomeA *A `json:"a,omitempty" bson:"a,omitempty"`
}
func main() {
b := B{}
b.X = 123
if buf, err := json.MarshalIndent(&b, "", " "); err != nil {
fmt.Println(err)
} else {
fmt.Println(string(buf))
}
}
Run Code Online (Sandbox Code Playgroud)
json编码器省略了SomeA属性,但在数据库中它就像那样"a" : null.我做错了什么,或者根本不可能这样做?
go ×10
arrays ×1
bson ×1
byte ×1
go-templates ×1
io ×1
json ×1
mgo ×1
reflection ×1
rune ×1
types ×1
unicode ×1
weak-typing ×1