小编mco*_*lin的帖子

Go/golang time.Now().UnixNano()转换为毫秒?

如何在几毫秒内获得Unix中的Unix时间?

我有以下功能:

func makeTimestamp() int64 {
    return time.Now().UnixNano() % 1e6 / 1e3
}
Run Code Online (Sandbox Code Playgroud)

我需要更少的精度,只需要几毫秒.

time go date-conversion unix-timestamp

65
推荐指数
5
解决办法
9万
查看次数

去检查自定义错误的类型

我很难在Go中使用自定义错误类型.我阅读了关于错误的博客文章

所以我尝试了这个:

在我的模型中.我定义了一个自定义错误:

type ModelMissingError struct {
    msg string // description of error
}

func (e *ModelMissingError) Error() string { return e.msg }
Run Code Online (Sandbox Code Playgroud)

在我的一个方法中,我抛出一个这样的自定义错误:

...
return Model{}, &ModelMissingError{"no model found for id"}
...
Run Code Online (Sandbox Code Playgroud)

在该方法的调用者中,我想检查为其类型返回的错误,如果它实际上是a,则采取措施ModelMissingError.

我怎样才能做到这一点?

我试过这个:

if err == model.ModelMissingError 
Run Code Online (Sandbox Code Playgroud)

结果是 *type model.ModelMissingError is not an expression*

显然我错过了一些东西.

error-handling go

24
推荐指数
6
解决办法
2万
查看次数

sed 替换 json 对象中键的值

我想做什么?

给定一个 json 事件文件。我想通过关键字定位特定事件,然后将该事件中的键值替换为“”。这必须使用 sed 来完成(Splunk 转发问题..我不会用细节来烦你)。

事件示例

{
  "message":"we have a response from SomeService",
  "other":"some stuff",
  "other2":"some other stuff",
  "xml":"<Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:awsse=\"http://xml.chicken.com/2010/06/Session_v3\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\"><Header><To>http://www.w3.org/2005/08/addressing/anonymous</To><From><Address>..... AND SO ON BIG GIANT NASTEY XML",
  "other3":"even more stuff"
}
Run Code Online (Sandbox Code Playgroud)

期望的结果

{
  "message":"we have a response from SomeService",
  "other":"some stuff",
  "other2":"some other stuff",
  "xml":"",
  "other3":"even more stuff"
}
Run Code Online (Sandbox Code Playgroud)

我尝试了什么? 我可以隔离事件并更换钥匙,没有问题。我正在努力使用正则表达式来替换json 中键的值。

cat test.json | sed '/"we have a response from SomeService"/ s/other2/chicken/'
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

regex json replace sed

5
推荐指数
1
解决办法
4636
查看次数

GitHub API文件更新:"未找到"

我试图通过API更新我的一个回购文件中的文件.

这是我得到的那个文件(效果很好):

curl -XGET 'https://git.fake.local/api/v3/repos/jsmith/repo_version/contents/version.html?ref=gh-pages'
{
  "name": "version.html",
  "path": "version.html",
  "sha": "b1b716105590454bfc4c0247f193a04088f39c7f",
  "size": 5,
  "url": "https://git.fake.local/api/v3/repos/jsmith/post_version/contents/version.html?ref=gh-pages",
  "html_url": "https://git.fake.local/jsmith/post_version/blob/gh-pages/version.html",
  "git_url": "https://git.fake.local/api/v3/repos/jsmith/post_version/git/blobs/b1b716105590454bfc4c0247f193a04088f39c7f",
  "type": "file",
  "content": "aW5pdAo=\n",
  "encoding": "base64",
  "_links": {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试通过PUT更新该文件:

curl -XPUT 'https://git.fake.local/api/v3/repos/jsmith/repo_version/contents/version.html?ref=gh-pages' -d '{
   "message": "update from api",
   "committer": {
     "name": "Joe Smith",
     "email": "jsmith@fake.com"
   },
   "content": "bXkgdXBkYXRlZCBmaWxlIGNvbnRlbnRz",
   "sha": "b1b716105590454bfc4c0247f193a04088f39c7f"
 }'
Run Code Online (Sandbox Code Playgroud)

结果:

{
  "message": "Not Found"
}
Run Code Online (Sandbox Code Playgroud)

api file-upload github fileupdate

3
推荐指数
1
解决办法
1508
查看次数

Django单元测试; 使用python-social-auth登录

我想为使用python-social-auth的 Django应用程序编写单元测试.运行Django并使用浏览器时,这一切都很棒,感谢python-social-auth!

但是,我似乎无法编写单元测试,因为我无法创建经过身份验证的客户端进行测试.

有没有人这么成功?
你是如何获得经过身份验证的客户端()的?

我试过这个(登录返回false并且不起作用):

self.c = Client()
self.u = User.objects.create(username="testuser", password="password", is_staff=True, is_active=True, is_superuser=True)
self.u.save()
self.auth = UserSocialAuth(user=self.u, provider="Facebook")
self.auth.save()
self.c.login(username=self.u.username, password=self.u.password) 
Run Code Online (Sandbox Code Playgroud)

python django unit-testing oauth django-authentication

2
推荐指数
1
解决办法
1737
查看次数

Opsworks:Rails Layer连接到Elasticache:Redis

我正在尝试将在Opsworks中运行的Rails应用程序连接到Elasticache Redis层.我无法让它发挥作用.

我目前的配置:

1个堆栈(2个实例)

- Rails App Server - MySQL

rails应用程序位于AWS-OpsWorks-Rails-App-Server安全组中.

1 ElasticCache群集ES群集位于default security sg-ff58559a (VPC)(active)安全组中.

我正在使用' Primary Endpoint '尝试连接.

可以从
ElastiCache>复制组
仪表板中看到此值.

它看起来类似于:
<name>.oveuui.ng.0001.use1.cache.amazonaws.com:6379

在我的rails控制台(在SSH进入rails层之后)我尝试:

>r = Redis.new(:url => 'redis://<name>.oveuui.ng.0001.use1.cache.amazonaws.com:6379')
>r.connected
Run Code Online (Sandbox Code Playgroud)

结果是:

Redis::CannotConnectError: Timed out connecting to Redis on...
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails amazon-web-services redis aws-opsworks

2
推荐指数
1
解决办法
2377
查看次数