在Go中实现的Web服务中,我希望能够根据用户的角色限制JSON响应中返回的字段.
例如,我可能有一个具有角色的当前登录用户和具有该角色的guest另一个用户admin
对于管理员,我希望json拥有所有密钥,例如
{
id: 1,
name: "John",
role: "admin"
}
Run Code Online (Sandbox Code Playgroud)
并且对于客人没有角色密钥,例如
{
id: 1,
name: "John"
}
Run Code Online (Sandbox Code Playgroud)
我现在可以编组json并返回所有字段.我需要能够限制它.
我的XML包含一组预定义元素,但我无法获取数组.这是XML结构:
var xml_data = `<Parent>
<Val>Hello</Val>
<Children>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
</Children>
</Parent>`
Run Code Online (Sandbox Code Playgroud)
这是完整的代码,这里是游乐场链接.运行它将获取Parent.Val,但不是Parent.Children.
package main
import (
"fmt"
"encoding/xml"
)
func main() {
container := Parent{}
err := xml.Unmarshal([]byte(xml_data), &container)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(container)
}
}
var xml_data = `<Parent>
<Val>Hello</Val>
<Children>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
</Children>
</Parent>`
type Parent struct {
Val string
Children []Child
}
type Child struct {
Val string
}
Run Code Online (Sandbox Code Playgroud)
编辑:我在这里简化了一下这个问题.基本上我不能解组任何数组,而不仅仅是预定义的结构.以下是更新的工作代码.在该示例中,只有一个项目在容器界面中结束.
func main() {
container := []Child{} …Run Code Online (Sandbox Code Playgroud) 我写了一个python模块。它驻留在一个回购。现在要运行模块的单元测试,它们不能是该模块的子文件夹。解决此问题的建议结构是:
ModuleRepo/
MyModule/
__init__.py
some_utils.py
import .some_helpers
some_helpers.py
Tests/
some_utils_test.py
import MyModule.some_utils
some_helpers_test.py
import MyModule.some_helpers
Run Code Online (Sandbox Code Playgroud)
现在效果很好。如果我运行测试,他们就可以导入模块。该模块能够通过前置 '.' 导入自己的文件(例如:some_helpers)。表示本地文件夹。
问题是一个不同的 repo 现在想要共享这个模块,我不知道如何让它找到该模块。
例如:
ModuleRepo/
MyModule/
__init__.py
...
Tests/
...
AnotherRepo/
using_my_module.py
import ??? <-- how to find MyModule?
Run Code Online (Sandbox Code Playgroud)
不工作的尝试 #1: 我最初尝试使用 git 的子模块功能将 ModuleRepo 包含在 AnotherRepo 下。但是我实际上并不想要ModuleRepo的根文件夹,我只想要子文件夹“MyModule”。事实证明 git 的子模块并没有这样做 - 一个人不能只选择要包含的回购的一部分。
不受欢迎的符号链接:虽然符号链接可能有效,但它不能“提交”到存储库,因此有些不受欢迎。此外,我正在 Windows 和 Linux 上进行开发,因此我需要一个适用于两者的解决方案。
可能的解决方案:将 ModuleRepo root 也变成一个模块(添加一个init .py)。然后我可以使用 git 使其成为 AnotherRepo 的子模块。我的导入会很丑,但它会是: import my.module.some_utils 而不是 import mymodule.some_utils
有没有人有更好的解决方案?
如果在我的项目中根本没有使用外部包中的函数,编译器是否会从生成的机器代码中删除该函数?
这个问题可以针对任何语言编译器.但是,我认为这种行为可能因语言而异.所以,我有兴趣知道编译器会做什么.
理解这一点,我将不胜感激.
任何人都知道是否可以修改,git因此它只会拒绝git stash并要求我明确指定git stash push. 当我想检查藏匿处 ( git stash list)时,我经常发现自己不小心藏了东西。同样,如果我在索引中有东西然后做git stash(忘记添加--keep-index),那就很烦人了。这反过来又会产生灾难性的后果(如何在 git stash / git stash pop 之后恢复索引?)
如何在 Windows 上为 Linux 编译 go 文件/项目?
在 Linux 上,它应该可以通过 one-liner 实现:
GOOS=windows go build -o filename.exe codefile.go
Run Code Online (Sandbox Code Playgroud)
我通过 Powershell 尝试了类似的东西,但在我的 Linux 服务器(Ubuntu 16.04 amd64)上只出现一个错误:
-bash: ./gotest: 无法执行二进制文件:Exec 格式错误”。
我在 Powershell 中尝试使用用户 Env-vars for GOOS = linuxandGOARCH = amd64和 via set GOOS = linux,但我对 Powershell 了解不够 - go build 命令运行时没有错误并生成文件。
任何人都可以使用 go-1.10 通过 Powershell(或 cmd)(通过 VS Code)解释 Windows(10 1709 x64)上的一般操作方法吗?
我正在研究一些Go的并发模式.我看着使用goroutine和输入/输出通道实现后台工作程序,并注意到当我将新作业发送到接收通道(基本上将新作业排入队列)时,我必须在goroutine中执行它,否则调度会搞乱.含义:
for _, jobData := range(dataSet) {
input <- jobData
}
Run Code Online (Sandbox Code Playgroud)
go func() {
for _, jobData := range(dataSet) {
input <- jobData
}
}()
Run Code Online (Sandbox Code Playgroud)
对于更具体的东西,我玩了一些无意义的代码(这里是去游乐场):
package main
import (
"log"
"runtime"
)
func doWork(data int) (result int) {
// ... some 'heavy' computation
result = data * data
return
}
// do the processing of the input and return
// results on the output channel
func Worker(input, output chan int) {
for data := range …Run Code Online (Sandbox Code Playgroud) 我尝试删除名为-dusing 命令
的 git 标记名称,git tag -d -d但无法删除它。
有任何想法吗?
我已使用Gmail API获取了邮件内容和必要的标头.我想把它们写入mbox文件.我可以找到Go包和样本来读取和解析mbox文件.但是如何使用Go创建和编写mbox文件?
我需要查询我的数据库以了解一小时内发生的事件。因此,我想获取从现在到那时(现在 - 24 小时,或现在 - 1 天)之间的事件。
我尝试过这种方法,但它是不正确的 -
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
// print the time now
fmt.Println(now)
then := time.Now()
diff := 24
diff = diff.Hours()
then = then.Add(-diff)
// print the time before 24 hours
fmt.Println(then)
// print the delta between 'now' and 'then'
fmt.Println(now.Sub(then))
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使then == 1 全天/24 小时之前?
非常感谢你的帮助!!
go ×7
git ×3
concurrency ×1
executable ×1
goroutine ×1
json ×1
powershell ×1
project ×1
python ×1
repository ×1
time ×1
xml ×1