Skip to content

Printing data frames

George Robinson edited this page Jan 9, 2023 · 3 revisions

You can print data frames with the following function:

// printField prints the value of the field.
func printField(v interface{}) {
        // You can add more cases to the switch to print other kinds of value
	switch v.(type) {
	case *float64:
		fmt.Printf("%f\n", *v.(*float64))
	default:
		fmt.Println(v)
	}
}

// printResponse prints all of the data frames in the response
func printResponse(data map[string]backend.DataResponse, separator string) {
	for refID, r := range data {
		fmt.Printf("Data frames for Ref ID: %s, num frames: %d\n", refID, len(r.Frames))
		for i, frame := range r.Frames {
			fmt.Print(strings.Repeat(separator, 1)) // indent
			fmt.Printf("Fields for frame: %d, num fields: %d\n", i, len(frame.Fields))
			for _, field := range frame.Fields {
				fmt.Print(strings.Repeat(separator, 2)) // indent
				fmt.Printf("Name: %s, Type: %s, Rows: %d\n", field.Name, field.Type(), field.Len())
				for j := 0; j < field.Len(); j++ {
					fmt.Print(strings.Repeat(separator, 3)) // indent
					printField(field.At(j))
				}
			}
		}
	}
}

when using \t as the separator:

Data frames for Ref ID: A, num frames: 1
	Fields for frame: 0, num fields: 2
		Name: Time, Type: []time.Time, Rows: 21
			2023-01-09 13:39:00 +0000 UTC
			2023-01-09 13:39:15 +0000 UTC
			2023-01-09 13:39:30 +0000 UTC
			2023-01-09 13:39:45 +0000 UTC
			2023-01-09 13:40:00 +0000 UTC
			2023-01-09 13:40:15 +0000 UTC
			2023-01-09 13:40:30 +0000 UTC
			2023-01-09 13:40:45 +0000 UTC
			2023-01-09 13:41:00 +0000 UTC
			2023-01-09 13:41:15 +0000 UTC
			2023-01-09 13:41:30 +0000 UTC
			2023-01-09 13:41:45 +0000 UTC
			2023-01-09 13:42:00 +0000 UTC
			2023-01-09 13:42:15 +0000 UTC
			2023-01-09 13:42:30 +0000 UTC
			2023-01-09 13:42:45 +0000 UTC
			2023-01-09 13:43:00 +0000 UTC
			2023-01-09 13:43:15 +0000 UTC
			2023-01-09 13:43:30 +0000 UTC
			2023-01-09 13:43:45 +0000 UTC
			2023-01-09 13:44:00 +0000 UTC
		Name: Value, Type: []*float64, Rows: 21
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
			5.000000
Data frames for Ref ID: B, num frames: 1
	Fields for frame: 0, num fields: 1
		Name: B, Type: []*float64, Rows: 1
			5.000000
Clone this wiki locally