草庐IT

nil_or_empty

全部标签

go - 为什么是 "err != nil"?

这个问题在这里已经有了答案:Hidingnilvalues,understandingwhyGofailshere(3个答案)关闭7年前。我遇到这样一种情况,变量“errerror”的值只能是“nil”,但在重新分配后断言“(err==nil)==false”。示例代码如下:packagemainimport("fmt""log")typeTestErrorstruct{Messagestring}func(e*TestError)Error()string{returne.Message}funcNewTestError(errerror)*TestError{iferr==nil{

xml - <nil> 在golang中解析xml字符串时

我想使用golang解析xml。作为使用go的新手,我阅读了网上的文章,解释了如何解析XML,但我不确定为什么在这种情况下我的返回值为nil。packagemainimport("fmt"//"io/ioutil""encoding/xml")funccheck(eerror){ife!=nil{panic(e)}}typeBooksstruct{XMLNamexml.Name`xml:"Books"`BookList[]Book`xml:"Books>Book"`}typeBookstruct{titlestring`xml:"title,attr"`authorstringpubl

regex - Go : strings or regexp 中短字符串前缀匹配的更好方法

我知道我得到的字符串会很短(我无法决定是使用strings.Contains,例如strings.Contains("123-ab-foo","123-ab"),还是正则表达式。我显然想要最快的方法。用例示例:ifstrings.Contains(current_string,MY_CONST){//dosmth} 最佳答案 如果您确定要查找的字符串(MY_CONST)将位于current_string的开头,那么最有效的方法将是HasPrefixfuncHasPrefix(s,prefixstring)boolHasPrefixt

arrays - golang 中包含整数和 nil 值的数组?

我是golang的新手。但我找不到这个问题的答案。在python中我可以做到,array=[1,2,3,None,5]但是当我写的时候就开始了vararray=[5]int{1,2,3,nil,5}编译器给我以下错误:cannotconvertniltotypeint我如何在golang中创建一个混合了整数和nil值的数组? 最佳答案 首先是一些理论。Python是一种动态类型语言。虽然每个单独的值都有一个类型,但您可以毫无问题地为变量分配任何类型的值。因此,python集合可以包含多种类型的值并且不会提示。以下是有效的Python

json.Unmarshal json string to object is empty 结果

这个问题在这里已经有了答案:PrintingEmptyJsonasaresult[duplicate](1个回答)json.Marshal(struct)returns"{}"(3个答案)关闭3年前。我有一个像这样的非常简单的程序:packagemainimport("encoding/json""fmt")typeRunCommandstruct{levelstring`json:"level"`callerstring`json:"caller"`msgstring`json:"msg"`cmdstring`json:"cmd"`}funcmain(){content:=`{"le

arrays - Go 中 []Foo(nil) 和 []Foo{} 的区别

我是Go的新手,想知道[]Foo(nil)和[]Foo{}之间的区别。(我在我的测试中使用它,我想指定当我的函数出错时,它应该返回nil,err。当我使用nil或[]Foo{},但在我使用[]Foo(nil)时有效。)我尝试过的我查看了Go文档和SO,发现了关于Foo{}但不是[]Foo(nil)的Struct内容。当我使用[]Foo{}时,测试失败输出:expected:[]Foo{}actual:[]Foo(nil)[]Foo(nil)和[]Foo{}的Fmt输出是相同的:fmt.Println([]Foo(nil))//[]fmt.Println([]Foo(){})//[]fm

sql - 使用 Go 和数据库/sql 扫描 nil 浮点值时出错

我正在编写一个程序,需要在对该表执行一些插入和更新之前确定该表的开盘值。有问题的表(在本例中为PostgreSql)最初可能有零行。当我选择期初值时,如果行数为零,则余额值的总和将返回为零。这会导致扫描失败并显示消息:Erroronscanoftest01openingRowCount.Error=sql:Scanerroroncolumnindex1:convertingstring""toafloat64:strconv.ParseFloat:parsing"":invalidsyntax虽然我可以通过做两个选择来“解决”问题,一个选择COUNT(*)另一个选择SUM()如果行数超

gcc - 尝试使用 sqlite3 驱动程序运行 sql 应用程序时出现 "No such file or directory"错误

packagemainimport("fmt"_"github.com/mattn/go-sqlite3")funcmain(){DB,err:=sql.Open("sqlite3","/Users/MyUser/Documents/GOProj/test.db")iferr!=nil{fmt.Printf("Error:%s\n",err)}deferDB.Close()}每次我运行这段代码(使用SublimeText3,MacOSX10.9)我得到这个:execgcc:Nosuchfileordirectory/usr/local/go/pkg/tool/darwin_amd64/

pointers - 调用结构函数给出 "cannot refer to unexported field or method"

我有这样的结构:typeMyStructstruct{Idstring}和函数:func(m*MyStruct)id(){//doingsomethingwithidhere}我还有一个这样的结构:typeMyStruct2struct{m*MyStruct}现在我有一个函数:funcfoo(str*MyStruct2){str.m.id()}但是我在编译时遇到错误:str.m.idundefined(cannotrefertounexportedfieldormethodmypackage.(*MyStruct)."".id如何正确调用这个函数? 最佳答案

go - panic : runtime error: invalid memory address or nil pointer dereference when running Blockchainr

我运行blockchain并获得以下终端输出:17:39:572015-06-16[INF]loadingdbleveldbpanic:runtimeerror:invalidmemoryaddressornilpointerdereferencepanic:runtimeerror:invalidmemoryaddressornilpointerdereference[signal0xbcode=0x1addr=0x0pc=0x402cb63]goroutine1[running]:main.search(0x4911ef8,0xc20806e2d0,0x0,0x0,0x4911ef