- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在从数据库中提取一些数据 - 我有一个指向 float32 的指针 - 因为如果我使用指针 - 那么我能够检查它是否为 nil(通常可能是 nil)。
当它不是 nil 时,我想获取值 - 如何取消引用它以便获取实际的 float32?我实际上无法在任何地方找到该链接!我确切地知道我想做什么,只是找不到 Go 中的语法,我对它还是很陌生 - 感谢所有帮助。
如果它是一个直接的 float32,我知道如何取消引用指针...
但是如果我有以下结构...
type MyAwesomeType struct{
Value *float32
}
然后在我这样做之后:
if myAwesomeType.Value == nil{
// Handle the error later, I don't care about this yet...
} else{
/* What do I do here? Normally if it were a straight float32
* pointer, you might just do &ptr or whatever, but I am so
* confused about how to get this out of my struct...
*/
}
最佳答案
The Go Programming Language Specification
For an operand x of pointer type *T, the pointer indirection *x denotes the variable of type T pointed to by x. If x is nil, an attempt to evaluate *x will cause a run-time panic.
使用 *
运算符。例如,
package main
import "fmt"
type MyAwesomeType struct {
Value *float32
}
func main() {
pi := float32(3.14159)
myAwesomeType := MyAwesomeType{Value: &pi}
if myAwesomeType.Value == nil {
// Handle the error
} else {
value := *myAwesomeType.Value
fmt.Println(value)
}
}
Playground :https://play.golang.org/p/8URumKoVl_t
输出:
3.14159
由于您是 Go 新手,请使用 A Tour of Go .游览解释了很多事情,包括指示。
Pointers
Go has pointers. A pointer holds the memory address of a value.
The type
*T
is a pointer to aT
value. Its zero value isnil
.var p *int
The
&
operator generates a pointer to its operand.i := 42
p = &iThe
*
operator denotes the pointer's underlying value.fmt.Println(*p) // read i through the pointer p
*p = 21 // set i through the pointer pThis is known as "dereferencing" or "indirecting".
Unlike C, Go has no pointer arithmetic.
关于go - 从结构内部的指向 float32 的指针获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50539159/
这个问题在这里已经有了答案: final keyword in method parameters [duplicate] (9 个回答) 关闭 8 年前。 在此示例中,声明 Object fina
我的目标:是通过我的函数更新字段获取选定值并使用函数输出值运行它。 问题:当我从列表中选择值时,它不会触发函数,也不会更新字段。 感谢您的帮助。 HTML 12 14 16 18 20 22 24
我有一本具有这种形式的字典: myDict = {'foo': bar, 'foobar baz': qux} 现在,我想拆分字典键中的空格,使其成为下一个键并获取值(重复)。 myDictRev1
vector a; vector b; int temp_holder; cout > temp_holder) a.push_back(temp_holder); cout > temp_h
Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿。当然,我
我正在使用 jquery ui 日期选择器来获取 fromDate 和 toDate 以下是from日期的代码 $("#from_date").datepicker({
我是一名优秀的程序员,十分优秀!