我想使用request.Body(type io.ReadCloser)包含图像的内容.
我不想使用,ioutil.ReadAll()因为我想将此主体直接写入文件以及想要解码它,所以我只想使用对内容的引用传递给进一步的函数调用,
我尝试创建多个读者实例,例如如下所示
package main
import (
"io/ioutil"
"log"
"strings"
)
func main() {
r := strings.NewReader("some io.Reader stream to be read\n")
a := &r
b := &r
log.Println(ioutil.ReadAll(*a))
log.Println(ioutil.ReadAll(*b))
}
Run Code Online (Sandbox Code Playgroud)
但在第二次通话中它总会产生nil.
请帮助我如何为同一个读者传递多个单独的参考?
我有以下架构
身份证(PK)| REF_ID | 活跃 | 地位
ID - 主键
我正在使用以下查询来选择和更新
BEGIN;
select * from table where ref_id = $1 and is_active is true for update;
UPDATE table set status = $1 where id =$2;
END;
Run Code Online (Sandbox Code Playgroud)
以上说明
1) Select 查询结果将用于锁定所有提供 ref ID 的行,该结果用于某些业务逻辑
2) 更新查询以更新属于同一 ref ID 的行的STATUS
问题
postgres@machine ERROR: deadlock detected
postgres@machine DETAIL: Process 28297 waits for ShareLock on transaction 4809510; blocked by process 28296.
Process 28296 waits for ShareLock on transaction 4809502; blocked by process …Run Code Online (Sandbox Code Playgroud) 我的服务是这样的
getRecords(): Observable<any>{
return this.http.get(this.fetchAdminData)
.map(this.extractData)
.catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)
和提取数据如下
private extractData(res: Response) {
let body = res.json();
return body || { };
}
Run Code Online (Sandbox Code Playgroud)
在我的组件中,我称之为如下
import {Component, OnInit} from '@angular/core'
import {FormsHandler} from '../services/service.forms'
@Component({
selector: 'form-admin',
templateUrl: '../partials/form5.html'
})
export class FormAdminComponent implements OnInit {
public records
constructor(private formHandler : FormsHandler){
}
ngOnInit(){
this.formHandler.getRecords().subscribe(res => {
if (res.ok){
this.records = res.data
console.log(res)
console.log(this.records[0])
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
但是当在html中调用如下时它会出错
{{records}} // this is working perfectly
{{records[0]}} //Cannot read property '0' of …Run Code Online (Sandbox Code Playgroud) 我通常尝试运行go test ./...CI/CD 管道的命令,该命令用于在所有子目录中运行所有测试用例。
但我最近重构了我的代码以从标志解析中获取配置文件路径,然后读取和初始化所有变量(在服务器启动之前需要)。此更改需要删除init function -> custom function将从主调用的所有初始化代码
。
现在一切都已完成,但所有测试用例都不起作用,因为我需要从某个地方调用所有自定义函数。
我尝试使用 TestMain 功能,但我认为它仅适用于同一个模块,并且所有测试用例都失败了。
func TestMain(m *testing.M) {
mySetupFunction()
retCode := m.Run()
myTeardownFunction()
os.Exit(retCode)
}
Run Code Online (Sandbox Code Playgroud)
需要帮助以了解如何创建全局测试设置或想知道是否有其他更好的方法进行上述重构。
我的模块结构如下
A
B
C
D
E
F
G
H
I
main.go
Run Code Online (Sandbox Code Playgroud)
其中父模块是 A,其中包含 main.go 和其他各种模块,它们嵌套在深层,并且所有模块都有自己的测试用例
我正在寻找连接并查询到PostgreSQL。但是我只想连接到特定的模式。
根据文档(JDBC),我们可以使用
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
Run Code Online (Sandbox Code Playgroud)
或更新为9.4,你可以指定像这样的新currentSchema参数的网址:
jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
Run Code Online (Sandbox Code Playgroud)
但是我无法使用golang SQL驱动程序执行此操作;
根据文档,我们也可以使用SET search_path TO myschema,public;
但我只想在初始化期间声明一次,但是我认为这需要为新连接每次执行。
另外我正在使用以下代码,请帮助我确定要传递给此参数的正确参数,以便仅与模式连接
db, err := sql.Open("postgres", `dbname=`+s.settings.Database+
` user=`+s.settings.Username+` password=`+s.settings.Password+
` host=`+s.settings.Url+` sslmode=disable`)
Run Code Online (Sandbox Code Playgroud)
正在添加currentSchema=myschema或searchpath=myschema无法正常运行!
有没有办法我只能连接到GO中的特定数据库架构
我正在迁移到 circleci2.0 并且在成功构建后执行测试时它们随机失败并显示以下错误消息
/usr/local/go/pkg/tool/linux_amd64/link: signal: killed
/usr/local/go/pkg/tool/linux_amd64/link: flushing $WORK/b462/payment_step_svc.test: write $WORK/b462/svc.test: cannot allocate memory
Run Code Online (Sandbox Code Playgroud)
我使用了以下配置
jobs:
build:
docker:
- image: circleci/golang:latest
- image: rabbitmq:3.5.4
- image: redis
working_directory: /go/src/github.com/soniabhishek/taskrunner
environment:
GOOS: linux
GOARCH: amd64
GOPATH: /go
steps:
- checkout
- run:
name: Get dependencies
command: go get -t -d -v ./...
- run:
name: Build all
command: go build ./...
- run:
name: Test all
command: go test -v ./...
Run Code Online (Sandbox Code Playgroud)
除了最新的(1.10.3)之外,我已经尝试过许多 golang 版本。
虽然我找到了这个黑客,但我不确定为什么会发生这种情况,当我使用CGO_ENABLED=0时,我的所有测试都会运行
想知道为什么会出现这个问题以及永久解决方案
我想按照下面提到的代码将一个对象插入到 Firestore 文档中。但它抛出了错误
.update({
"comments": firebase.fieldValue.arrayUnion({
userId,
comment: data.comment,
createdAt: firebase.fieldValue.serverTimestamp(),
})
Run Code Online (Sandbox Code Playgroud)
错误:
FirebaseError:使用无效数据调用函数 FieldValue.arrayUnion()。FieldValue.serverTimestamp() 只能与 update() 和 set() 一起使用(在文档中找到...)
如何向推送的数组对象添加时间戳,或者是否有其他方法可以实现类似的结果?我从上面的错误中得到什么是我不能使用里面的时间戳arrayUnion
我正在尝试制作一个程序,它在文件中提供偏移量,就像我可以从第20个偏移量开始写入等.
这是我用作参考的示例代码之一
package main
import (
"fmt"
"io/ioutil"
"os"
)
const (
filename = "sample.txt"
start_data = "12345"
)
func printContents() {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
fmt.Println("CONTENTS:", string(data))
}
func main() {
err := ioutil.WriteFile(filename, []byte(start_data), 0644)
if err != nil {
panic(err)
}
printContents()
f, err := os.OpenFile(filename, os.O_RDWR, 0644)
if err != nil {
panic(err)
}
defer f.Close()
if _, err := f.Seek(20, 0); err != nil {
panic(err)
}
if …Run Code Online (Sandbox Code Playgroud) 嗨,我根据我的用例使用 mongo 和 golang 我想在插入之前生成一个 _id,因为我正在使用
bson.NewobjectId()
Run Code Online (Sandbox Code Playgroud)
我的结构有点像这样
type Device struct {
Id bson.ObjectId `bson:"_id" json:"_id,omitempty"`
UserId string `bson:"userId" json:"userId"`
CategorySlug string `bson:"categorySlug" json:"categorySlug"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
ModifiedAt time.Time `bson:"modifiedAt" json:"modifiedAt"`
BrandId int `bson:"brandId" json:"brandId"`
Category string `bson:"category" json:"category"`
}
Run Code Online (Sandbox Code Playgroud)
当我使用这个 json 请求时
{
"userId" : "gKn42jJD8uy8ksQpi",
"categorySlug" : "television",
"createdAt" : "2016-08-25T18:47:29.558Z",
"modifiedAt" : "2016-08-25T18:47:29.558Z",
"brandId" : 90,
"category" : "LED TV",
"dateOfPurchase" : "2016-08-25T18:47:29.558Z"
}
Run Code Online (Sandbox Code Playgroud)
并将其解码为设备类型,然后使用 decode.ID = bson.NewObjectId() 初始化我的 id
但是当我查看我的数据库时,插入的值奇怪地以这种形式出现
{
"_id" : ObjectId("57bf425a34ce5ee85891b914"),
"0" …Run Code Online (Sandbox Code Playgroud) go ×6
postgresql ×2
angular ×1
circleci-2.0 ×1
file-writing ×1
firebase ×1
insert ×1
mongodb ×1
reader ×1
sql-update ×1
transactions ×1
typescript ×1