小编Bri*_*ker的帖子

golang嵌入文件以便以后解析执行使用

我基本上试图浏览一个html文件的文件夹.我想将它们嵌入到二进制文件中,并能够根据模板执行目的请求解析它们.(如果我没有正确措辞,请原谅我).

任何想法,提示,技巧或更好的方法来实现这一点非常感谢.

// Template Files
type TempFiles struct {
    Files map[string]string
}

// Loop through view files and load them
func LoadTempFiles() {
    t := new(TempFiles)

    // Load template files
    filepath.Walk("application/views", func(path string, info os.FileInfo, err error) error {
    if !info.IsDir() {
        content, _ := ioutil.ReadFile(path)
        t.Files[path] = string(content)
    }
    return nil
    })
}

func ViewTemp(w http.ResponseWriter, path string) {
    t := new(TempFiles)

    temp, err := template.New().Parse(t.Files[path])
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    } else {
        temp.Execute(w, nil) …
Run Code Online (Sandbox Code Playgroud)

go

41
推荐指数
4
解决办法
1万
查看次数

版本化Mysql数据(不仅仅是架构)

我的办公室一直在谈论创建一个版本控制mysql数据(而不是模式/迁移)的软件包.

基本上这个过程会像这样工作.请记住,客户端仍然像往常一样使用后端,然后像使用wordpress后端一样使用它.客户端将登录选择一个"分支"给它一个名称让我们说"新用户"这将克隆一个全新的数据库,允许用户在那里工作"分支"而不影响实时.一旦客户端完成数据更改,他们就会将数据分支合并到"主"(实时)中.

在合并时,它会将实时和"新用户"分支数据导出到sql文件并执行svn diff并合并更改.

引发这种想法的情况是,如果我们的客户需要对网站进行一系列更改,但不想将这些数据置于实际状态,并且当他们进行更改时,他们也不希望影响其他同事网站更改.基本上复制了开发人员在Git等存储库中工作时所做的事情.

此外,如果客户端在开发/演示站点上工作,他们希望将他们的工作放在现场.

我想开启讨论,以了解这是否是一个好主意?我们可能会遇到什么问题?在处理数据时,这是一个很好的编程实践吗?这样的事情已经存在吗?

php mysql svn git

9
推荐指数
1
解决办法
231
查看次数

全局模板数据

在做的时候,ExecuteTemplate我看到所有&whateversruct{Title: "title info", Body: "body info"}用于将数据发送到模板以替换信息的示例.我想知道是否有可能不必在我的处理函数之外创建一个结构,因为我拥有的每个处理函数都不会有相同的Title,Body.能够发送一个替换模板信息的地图会很高兴.有什么想法或想法吗?

目前 - 写得很松散

type Info struct {
    Title string
    Body string
}

func View(w http.ResponseWriter) {
    temp.ExecuteTemplate(w, temp.Name(), &Info{Title: "title", Body: "body"})
}
Run Code Online (Sandbox Code Playgroud)

看起来好像创建结构是不必要的.对于您创建的每个函数,结构都不相同.所以你必须为每个函数创建一个结构(我知道).

go go-templates

4
推荐指数
3
解决办法
7930
查看次数

golang变量设置

我需要在我的包的开头设置一个变量,稍后将用parseFiles()填充,但我不知道如何设置变量,因为它不是字符串或int或类似的泛型.我如何设置变量而不必在那里添加一些任意名称来设置它?

var templatefiles = template.New("foo") // Im having to do New("foo") just to set the variable

// Later on adding files to original variable
templatefiles.New("template name").Parse("Template text here")
Run Code Online (Sandbox Code Playgroud)

go

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

golang 传递 http.ResponseWriter

我试图弄清楚是否有可能在编写 Web 应用程序时不必随处传递 http.ResponseWriter。我正在设置一个简单的 mvc web 框架,我发现自己必须通过各种函数传递 http.ResponseWriter 当它只用于让我们说最后一个函数时。

路线包

// Struct containing http requests and variables
type UrlInfo struct {
    Res http.ResponseWriter
    Req *http.Request
    Vars map[string]string
}

func HandleFunc(handlepath string, runfunc func(*UrlInfo)) {
    // Set handler and setup struct
    http.HandleFunc(getHandlePath(handlepath), func(w http.ResponseWriter, r *http.Request) {
        url := new(UrlInfo)
        url.Res = w
        url.Req = r
        url.Vars = parsePathVars(r.URL.Path, handlepath)

        runfunc(url)
    })
}

// Parse file and send to responsewriter
func View(w http.ResponseWriter, path string, data interface{}) {
    // Go grab …
Run Code Online (Sandbox Code Playgroud)

go

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

golang导入结构指针

好的,我有一个主包和一个http处理程序包。本质上,我想做的是设置一个全局结构,以便我可以随时调用该结构中的信息。

下面是我尝试的示例的基本概述:主程序包导入处理程序函数主程序包调用handlerfunc Handlerfunc将http.ResponseWriter和其他项设置为UrlInfo结构Handlerfunc在函数中传递(不必将UrlStruct传递到函数中)Run函数(在此示例中为home)函数home可以随时调用变量uinfo导致其指针UrlInfo结构

显然这是行不通的,但这本质上是我想这样做的方式,因此我不必将所有这些信息传递到我的home函数中。保持清洁和简单。

任何想法都值得欢迎。谢谢。

处理程序包

package handler

// Struct containing http requests and variables
type UrlInfo struct {
    Res http.ResponseWriter
    Req *http.Request
    Item string
}

func HandleFunc(handlepath string, runfunc func()) {
    // Set handler and setup struct
    http.HandleFunc(handlepath, func(w http.ResponseWriter, r *http.Request) {
        url := new(UrlInfo)
        url.Res = w
        url.Req = r
        url.Item = "Item information"

        runfunc()
    })
}
Run Code Online (Sandbox Code Playgroud)

主包装

import "handler"

var uinfo = &handler.UrlInfo{}

func init() {
    handler.HandleFunc("/home/", home)
}

func home() {
    fmt.Println(uinfo.Item)
}
Run Code Online (Sandbox Code Playgroud)

go

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

标签 统计

go ×5

git ×1

go-templates ×1

mysql ×1

php ×1

svn ×1