英文:
How to print the data in a frame using an array?
问题
我正在尝试使用olog
库将数据打印到一个框架中。当我在struct
中使用字符串变量,并在[]Data{}
中使用它们时,它会将数据打印到框架中。但是,当我在struct
中使用TicketHeading
数组,并在[]Data{}
中使用它时,会出现以下错误。
错误
go: 找不到 github.com/da0x/golang/olog 包的模块
go: 正在下载 github.com/da0x/golang v1.0.4
./prog.go:17:23: 在结构体字面值中,dt.TicketHeading[0] 字段名无效
./prog.go:18:23: 在结构体字面值中,dt.TicketHeading[1] 字段名无效
代码
package main
import "github.com/da0x/golang/olog"
type Data struct {
TicketHeading [2]string
Ticket_title, Ticket_description string
}
func main() {
var dt Data
dt.TicketHeading = [2]string{"Ticket_titles", "Ticket_descriptions"}
var data = []Data{
{dt.TicketHeading[0]: "This is the ticket heading"},
{dt.TicketHeading[1]: "This is the ticket description"},
}
olog.Print(data)
}
英文:
I am trying to print the data in a frame using the olog
library. When I use the string variables inside a struct
and use them in the []Data{}
, it prints the data in a frame but when I use the TicketHeading
array in a struct and use it inside the []Data{}
, it gives me the following error.
Error
go: finding module for package github.com/da0x/golang/olog
go: downloading github.com/da0x/golang v1.0.4
./prog.go:17:23: invalid field name dt.TicketHeading[0] in struct literal
./prog.go:18:23: invalid field name dt.TicketHeading[1] in struct literal
Code
package main
import "github.com/da0x/golang/olog"
type Data struct {
TicketHeading [2]string
Ticket_title, Ticket_description string
}
func main() {
var dt Data
dt.TicketHeading = [2]string{"Ticket_titles", "Ticket_descriptions"}
var data = []Data{
{dt.TicketHeading[0]: "This is the ticket heading"},
{dt.TicketHeading[1]: "This is the ticket description"},
}
olog.Print(data)
}
答案1
得分: 1
你最初使用的库很好地完成了它承诺要做的任务,根据它的GitHub页面https://github.com/da0x/olog
它可以将任何结构体数组组装成一个ASCII框。
它只打印结构体。如果你想打印一些动态加载的数据,比如从文件中加载数据,可以看一下这个库https://github.com/kataras/tablewriter。我想下面的代码片段很适合你:
package main
import (
"os"
"github.com/olekukonko/tablewriter"
)
func main() {
data := [][]string{
{"A", "The Good", "500"},
{"B", "The Very very Bad Man", "288"},
{"C", "The Ugly", "120"},
{"D", "The Gopher", "800"},
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Sign", "Rating"})
for _, v := range data {
table.Append(v)
}
table.Render()
}
英文:
The library you took initially does well the task it promises to do, as per its GitHub page https://github.com/da0x/olog
> It takes any array of structs and assembles an ascii box around the
> objects
It prints structs only. In case you want to print some data loaded dynamically, from a file on instance, look at this library https://github.com/kataras/tablewriter. I guess the snippet below is a good fit for you:
package main
import (
"os"
"github.com/olekukonko/tablewriter"
)
func main() {
data := [][]string{
{"A", "The Good", "500"},
{"B", "The Very very Bad Man", "288"},
{"C", "The Ugly", "120"},
{"D", "The Gopher", "800"},
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Sign", "Rating"})
for _, v := range data {
table.Append(v)
}
table.Render()
}
答案2
得分: 0
你使用的库会根据你提供的数据结构字段的名称来推断列名,这里是Data
。所以要命名为"Ticket_titles"和"Ticket_description"这样的列名,你只需要在Data
结构中有这样的字段名称即可。不需要单独提供列名(标题),就像你在dt.TicketHeading = [2]string{"Ticket_titles", "Ticket_descriptions"}
中所做的那样。
请看下面的示例代码片段:
package main
// import "fmt"
import "github.com/da0x/golang/olog"
type Data struct {
// TicketHeading [2]string
Ticket_title, Ticket_description string
}
func main() {
var data = []Data{
{"Title 1", "Description 1"},
{"Title 2", "Description 2"},
}
olog.Print(data)
}
它给我输出的结果是:
> go run .
┌────────────┬──────────────────┐
│Ticket_title│Ticket_description│
├────────────┼──────────────────┤
│Title 1 │Description 1 │
│Title 2 │Description 2 │
└────────────┴──────────────────┘
你可以参考GitHub页面获取更多详细信息:https://github.com/da0x/olog
英文:
The library you use infers column names from names of data structure fields you provide, Data
in this case. So to name columns like "Ticket_titles" and "Ticket_description" you just need to have fields with such names in your Data
structure. No need to provide column names (header) separately as you did dt.TicketHeading = [2]string{"Ticket_titles", "Ticket_descriptions"}
.
Look at my example snippet below:
package main
// import "fmt"
import "github.com/da0x/golang/olog"
type Data struct {
// TicketHeading [2]string
Ticket_title, Ticket_description string
}
func main() {
var data = []Data{
{"Title 1", "Description 1"},
{"Title 2", "Description 2"},
}
olog.Print(data)
}
It gives me this output:
> go run .
┌────────────┬──────────────────┐
│Ticket_title│Ticket_description│
├────────────┼──────────────────┤
│Title 1 │Description 1 │
│Title 2 │Description 2 │
└────────────┴──────────────────┘
You can refer to GitHub page for more details: https://github.com/da0x/olog
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论