package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
Run Code Online (Sandbox Code Playgroud)
命令是go run a.go.
此代码在 Windows 中需要 4~5 秒。
好吧用文字描述它很难,但是假设我有一个存储int指针的地图,并希望将操作的结果存储为我的哈希中的另一个键:
m := make(map[string]*int)
m["d"] = &(*m["x"] + *m["y"])
Run Code Online (Sandbox Code Playgroud)
这不起作用,并给我错误: cannot take the address of *m["x"] & *m["y"]
思考?
我尝试过len()函数,但它给出了声明的值.该size()函数给出了错误.
码:
package main
var check [100]int
func main() {
println(len(check))
}
Run Code Online (Sandbox Code Playgroud)
输出在100这里,我需要数组中的总项目(即0).
我是Go的菜鸟:)所以我的问题可能是愚蠢的,但找不到答案,所以.
我需要一个功能:
func name (v interface{}) {
if is_slice() {
for _, i := range v {
my_var := i.(MyInterface)
... do smth
}
} else {
my_var := v.(MyInterface)
... do smth
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能is_slice去Go?感谢任何帮助.
我定义了这些类型:
func init() {
gob.RegisterName("MyMessageHeader", MyMessageHeader{})
gob.RegisterName("OtherMsg", OtherMsg{})
}
//
// Messages
//
type MyMessageHeader struct {
MessageId InstanceIdType
OtherId uint64
}
type MyMessage interface {
Id() *MyMessageHeader
}
type otherStruct struct {
X uint8
Y uint8
}
type OtherMsg struct {
MyMessageHeader
OtherField *otherStruct
}
func (m *OtherMsg) Id() *MyMessageHeader {
return &m.MyMessageHeader
}
func MarshalMessage(m MyMessage) ([]byte, error) {
var buff bytes.Buffer
encoder := gob.NewEncoder(&buff)
if err := encoder.Encode(&m); err != nil {
return nil, errors.Newf("Could not marshal …Run Code Online (Sandbox Code Playgroud) 我在App引擎上有一个servlet来提供图像.
servlet正确设置HTTP标头值以指示应缓存图像.但App Engine会覆盖这些标题,导致图像不被缓存.
请注意,之前相同的代码工作,但现在它不起作用.
App Engine的文档指出,如果Cache-Control,Expires和Vary报头由servlet设置,他们将留不变:
https://developers.google.com/appengine/docs/java/runtime#Responses
这是我的示例代码:
response.setContentType( "image/jpeg" );
//response.setDateHeader( "Expires", new Date().getTime() + 60L*24*60*60*1000 ); // 60 days cache time
//response.addHeader( "Cache-Control", "public, max-age=5184000" ); // 5_184_000 sec = 60 days cache time
response.addHeader( "Cache-Control", "public, max-age=90000" ); // 90_000 sec = 25 hours cache time
response.getOutputStream().write( data ); // Data is a byte array containing the JPEG image data
Run Code Online (Sandbox Code Playgroud)
(我已经尝试过把所有内容都注释掉了.)
检查HTTP请求 - 响应,响应包含以下标头:
HTTP/1.1 200 OK
status: 200 OK
version: …Run Code Online (Sandbox Code Playgroud) String s = null;
s = s + "hai";
System.out.println(s);
Run Code Online (Sandbox Code Playgroud)
输出:
nullhai
Run Code Online (Sandbox Code Playgroud)
以为这会让我觉得NPE.
什么是背后的基本逻辑
+(连接)时不投掷NPE.charCodeAt()JavaScript中的方法返回给定索引处字符的数字Unicode值,例如
"s".charCodeAt(0) // returns 115
Run Code Online (Sandbox Code Playgroud)
我将如何获取Go中相同字符串/字母的数字unicode值?
在xml:Unmarshal的Go 文档中,有一个解组此xml的示例
<Person>
<FullName>Grace R. Emlin</FullName>
<Company>Example Inc.</Company>
<Email where="home">
<Addr>gre@example.com</Addr>
</Email>
<Email where='work'>
<Addr>gre@work.com</Addr>
</Email>
<Group>
<Value>Friends</Value>
<Value>Squash</Value>
</Group>
<City>Hanga Roa</City>
<State>Easter Island</State>
</Person>
Run Code Online (Sandbox Code Playgroud)
使用这些结构
type Address struct {
City, State string
}
type Result struct {
XMLName xml.Name `xml:"Person"`
Name string `xml:"FullName"`
Phone string
Email []Email
Groups []string `xml:"Group>Value"`
Address
}
Run Code Online (Sandbox Code Playgroud)
注意,Result包含对单独定义的引用Address.显然,这段代码有效.
当我试图解组这个xml
<C>
<D>
<E>Fred</E>
<F>42</F>
</D>
</C>
Run Code Online (Sandbox Code Playgroud)
使用这些结构
type D struct {
E string
F int
}
type C …Run Code Online (Sandbox Code Playgroud) 我正在处理优惠券表格,其中有一些可选字段。
介绍:
所有表单字段值都作为 JSON 接收并映射到 Golang 结构中。在结构中,我为每个字段添加了一个“omitempty”标志。因此,只有那些具有适当值的表单值才会被映射,其余的值(如 0、“”、false)将被结构忽略。
这是Golang的结构
type Coupon struct {
Id int `json:"id,omitempty" bson:"_id,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Code string `json:"code,omitempty" bson:"code,omitempty"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
Status bool `json:"status" bson:"status"`
MaxUsageLimit int `json:"max_usage_limit,omitempty" bson:"max_usage_limit,omitempty"`
SingleUsePerUser bool `json:"single_use_per_user,omitempty" bson:"single_use_per_user,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)
问题:
当我第一次保存这个表单时,合适的表单值会保存到 Mongodb 中。
现在我想更新该表单并假设有一个复选框,该复选框在保存数据时已选中。更新表单时,未选中该复选框并提交表单以保存。现在因为我在结构中应用了“omitempty”标志,所以它没有将空值映射到复选框字段。由于该值未映射到结构中,因此不会保存到数据库中。
当用户第二次编辑表单时,它会看到相同的复选框被选中。(但实际上,该值应更新到数据库中,并且复选框应显示为未选中状态。)
我在 REST API 中使用相同的表单数据(JSON 格式)。在 API 中,在更新表单数据时,如果我只提到那些必需的值并且不传递我不想更新的值,那么 MongoDB 就会使用提供的必需值覆盖整个文档(即使这些值是也被覆盖,我不想更新也不想传入 API)。
要求:
将来,我想公开 REST API,所以我不希望这件事发生在那里。这就是为什么我不想从结构字段中删除“omitempty”标志。
在结构中使用 omitempty 标志时,有什么方法可以将空表单值或 API 数据字段保存到数据库中?
谢谢!