Golang中如何实现这样的查询结果

Golang中如何实现这样的查询结果

var data = &a{
Title: []string{"One", "Two", "Three"},
Article: [][]string{
	[]string{"a", "b", "c","b"},
	[]string{},
	[]string{"f", "g", "h", "i"}},
	}
6 回复

抱歉,但我没理解你的示例。

你能添加一些格式让它更容易阅读吗?比如一些与你期望结果等价的代码,或者你尝试实现该结构的方式等等?

更多关于Golang中如何实现这样的查询结果的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


这是字段的更改

Untitled

我不太确定先生,但我试图实现的是这样的…

标题:一

文章:a.

文章:b.

文章:c.

文章:b.

标题:二

标题:三

文章:f.

文章:g.

文章:h.

文章:i.

所以如果我理解正确的话,表1中的条目与表2存在一对多关系。

表2使用标题来引用表1,并且自身没有唯一标识符?

此外,您希望将表1的内容切片与表2的内容列表直接并列,并使用相同的索引来访问相关联的内容?

您应该立即停止并重新设计数据库。

  1. 在表2中使用适当的唯一ID,即使目前看起来没有必要
  2. 在表2中使用表1的ID作为外键,而不是标题
  3. 为单个条目使用结构体(例如type Table1 struct {Title string; Article []Table2}(当然也需要正确定义Table2))
  4. 如果要查询表1的多个内容,请返回[]Table1而不是您最初提议的结构
type Table1 struct {Title string; Article []Table2}
package main

import (
	"html/template"
	"os"
	"fmt"
)

type a struct {
	Title   []string
	Article [][]string
}

var data = &a{
	Title: []string{"One", "Two", "Three"},
	Article: [][]string{
		[]string{"a", "b", "c","b"},
		[]string{},
		[]string{"f", "g", "h", "i"}},
}


var tmplSrc = `
{{range $i, $a := .Title}}
  Title: {{$a}}
  {{range $article := index $.Article $i}}
	Article: {{$article}}.
  {{end}}
{{end}}`

func main() {
fmt.Println(data)
	tmpl := template.Must(template.New("test").Parse(tmplSrc))
	tmpl.Execute(os.Stdout, data)
}
package main

import (
	"html/template"
	"os"
	"fmt"
)

type a struct {
	Title   []string
	Article [][]string
}

var data = &a{
	Title: []string{"One", "Two", "Three"},
	Article: [][]string{
		[]string{"a", "b", "c","b"},
		[]string{},
		[]string{"f", "g", "h", "i"}},
}


var tmplSrc = `
{{range $i, $a := .Title}}
  Title: {{$a}}
  {{range $article := index $.Article $i}}
	Article: {{$article}}.
  {{end}}
{{end}}`

func main() {
fmt.Println(data)
	tmpl := template.Must(template.New("test").Parse(tmplSrc))
	tmpl.Execute(os.Stdout, data)
}

我想替换这个

var data = &a{
			Title: []string{"One", "Two", "Three"},
			Article: [][]string{
				[]string{"a", "b", "c","b"},
				[]string{},
				[]string{"f", "g", "h", "i"}},
		}

为SQL查询,表格如下所示 Untitled

先谢谢了

在Go语言中,您提供的结构体定义和初始化方式可以通过结构体类型来实现。以下是一个完整的示例代码,展示如何定义结构体、初始化数据以及如何访问和打印这些数据:

package main

import "fmt"

// 定义结构体类型
type a struct {
	Title   []string
	Article [][]string
}

func main() {
	// 初始化结构体
	var data = &a{
		Title: []string{"One", "Two", "Three"},
		Article: [][]string{
			{"a", "b", "c", "b"},
			{},
			{"f", "g", "h", "i"},
		},
	}

	// 访问和打印数据
	fmt.Println("Titles:")
	for i, title := range data.Title {
		fmt.Printf("  %d: %s\n", i, title)
	}

	fmt.Println("\nArticles:")
	for i, article := range data.Article {
		fmt.Printf("  %d: %v\n", i, article)
	}

	// 访问特定元素示例
	fmt.Printf("\nFirst title: %s\n", data.Title[0])
	fmt.Printf("Second article: %v\n", data.Article[1])
	fmt.Printf("Third article's first element: %s\n", data.Article[2][0])
}

运行此代码将输出:

Titles:
  0: One
  1: Two
  2: Three

Articles:
  0: [a b c b]
  1: []
  2: [f g h i]

First title: One
Second article: []
Third article's first element: f

这个示例展示了:

  1. 使用结构体类型a来组织标题和文章数据
  2. 使用切片来存储字符串数组
  3. 通过指针&a{}来创建结构体实例
  4. 使用循环遍历和访问切片中的元素
  5. 演示了如何访问特定的数组元素

如果您需要进一步处理这些数据,比如查询、过滤或转换,可以根据具体需求添加相应的逻辑。

回到顶部