函数返回时的内存驻留问题
时间:2024-02-29
浏览:208
作者:佚名
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《函数返回时的内存驻留问题》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
问题内容
我正在程序中使用 go-git 并尝试测试我的功能。为了测试一个函数,我想首先创建一个内存存储库,向其中提交一个文件,然后让我的函数使用它。
因此,在我的测试中,我编写了一个帮助程序,可以在内存文件系统中创建新的内存存储库(init)并返回它。
但是当我尝试在调用函数中使用存储库时,我什么也没得到。
这是重现问题的示例:
package main
import (
"fmt"
"os"
"time"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/storage/memory"
)
func maketemprepo() (*git.repository, billy.filesystem, error) {
s := memory.newstorage()
f := memfs.new()
r, err := git.init(s, f)
if err != nil {
return nil, nil, fmt.errorf("failed to create in-memory repo: %v", err)
}
readme, err := f.create("/readme.md")
if err != nil {
return nil, nil, fmt.errorf("failed to create a file in repository: %v", err)
}
readme.write([]byte("hello world"))
w, _ := r.worktree()
_, err = w.add("/readme.md")
if err != nil {
return nil, nil, fmt.errorf("failed to add file to repository: %v", err)
}
commit, err := w.commit("test commit", &git.commitoptions{
author: &object.signature{
name: "john doe",
email: "[email protected]",
when: time.now(),
},
})
if err != nil {
return nil, nil, fmt.errorf("failed to commit file to repository: %v", err)
}
obj, err := r.commitobject(commit)
if err != nil {
return nil, nil, fmt.errorf("failed to get commit: %v", err)
}
if obj == nil {
return nil, nil, fmt.errorf("commit object is nil")
}
fmt.printf("%v\n", obj)
return r, f, nil
}
func clone(url, tagprefix string, fs billy.filesystem) (*git.repository, error) {
r, err := git.clone(memory.newstorage(), fs, &git.cloneoptions{
url: url,
})
if err != nil {
return nil, fmt.errorf("failed to clone repo '%s': %v", url, err)
}
err = r.fetch(&git.fetchoptions{
force: true,
refspecs: []config.refspec{
config.refspec("refs/*:refs/*"),
config.refspec("head:refs/heads/head"),
},
})
if err != nil {
return nil, fmt.errorf("failed to fetch repo '%s': %v", url, err)
}
return r, nil
}
func main() {
repo, fs, err := maketemprepo()
if err != nil {
fmt.printf("could not create temp repo: %v", err)
os.exit(1)
}
commits, err := repo.commitobjects()
if err != nil {
fmt.printf("unable to get commits: %v", err)
os.exit(1)
}
fmt.println("commits:")
for c, err := commits.next(); err != nil; {
fmt.printf("a commit: %v\n", c)
os.exit(1)
}
res, err := clone(fs.root(), "foo", fs)
if err != nil {
fmt.printf("unexpected error: %v", err)
os.exit(1)
}
h, err := res.head()
if err != nil {
fmt.printf("error getting head: %v", err)
os.exit(1)
}
if h == nil {
fmt.println("missing head commit")
os.exit(1)
}
fmt.printf("head: %v", h)
}
执行它会给出:
$ go run main.go
commit 936fa4014b70e008bf01338d1c0916e1365f77a6
Author: John Doe <[email protected]>
Date: Fri Dec 07 12:17:39 2018 +0100
test commit
commits:
unexpected error: failed to clone repo '/': repository not foundexit status 1
因此,main 函数无法列出存储库中的任何提交,也无法克隆它。
我做错了什么?
解决方案
问题出在 clone 方法中。
URL 选项必须指向 git 存储库,但 fs 仅包含 README.md 文件(尝试查看 fs.ReadDir("/") 的输出),因此 fs.Root() 不是有效的 URL 选项。
fs 没有 .git(请参阅 comment),并且所有 git 内容都在您的 Storer 中。
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
