[GH-ISSUE #88] code understanding for textview|Highlight using empty struct #68

Closed
opened 2026-03-04 01:01:38 +03:00 by kerem · 1 comment
Owner

Originally created by @stephencheng on GitHub (Mar 28, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/88

I haven't had experience using the empty struct in go lang yet, though I understand there are a few benefits, I couldn't understand what purpose is to use the empty struct like below code.

The highlights is a map with id as key, but non value at all. Is it to achieve the goal of quick query if the id exist using the line like this?

if _, ok := t.highlights[regionID]; ok


// Highlight specifies which regions should be highlighted. See class
// description for details on regions. Empty region strings are ignored.
//
// Text in highlighted regions will be drawn inverted, i.e. with their
// background and foreground colors swapped.
//
// Calling this function will remove any previous highlights. To remove all
// highlights, call this function without any arguments.
func (t *TextView) Highlight(regionIDs ...string) *TextView {
	t.highlights = make(map[string]struct{})
	for _, id := range regionIDs {
		if id == "" {
			continue
		}
		t.highlights[id] = struct{}{}
	}
	t.index = nil
	return t
}

Originally created by @stephencheng on GitHub (Mar 28, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/88 I haven't had experience using the empty struct in go lang yet, though I understand there are a few benefits, I couldn't understand what purpose is to use the empty struct like below code. The highlights is a map with id as key, but non value at all. Is it to achieve the goal of quick query if the id exist using the line like this? > if _, ok := t.highlights[regionID]; ok ``` // Highlight specifies which regions should be highlighted. See class // description for details on regions. Empty region strings are ignored. // // Text in highlighted regions will be drawn inverted, i.e. with their // background and foreground colors swapped. // // Calling this function will remove any previous highlights. To remove all // highlights, call this function without any arguments. func (t *TextView) Highlight(regionIDs ...string) *TextView { t.highlights = make(map[string]struct{}) for _, id := range regionIDs { if id == "" { continue } t.highlights[id] = struct{}{} } t.index = nil return t } ```
kerem closed this issue 2026-03-04 01:01:38 +03:00
Author
Owner

@rivo commented on GitHub (Mar 28, 2018):

Correct. Go doesn't have built-in sets so a map with no values essentially turns it into a set. An empty struct uses no space so map[string]struct{} is the most efficient (simple) way to do it.

In line 583, I'm checking if a region is highlighted by querying the map.

<!-- gh-comment-id:376935200 --> @rivo commented on GitHub (Mar 28, 2018): Correct. Go doesn't have built-in sets so a map with no values essentially turns it into a set. An empty struct uses no space so `map[string]struct{}` is the most efficient (simple) way to do it. In [line 583](https://github.com/rivo/tview/blob/87ba87fedadb72363f842e5f9b67bf697d9bce0e/textview.go#L583), I'm checking if a region is highlighted by querying the map.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#68
No description provided.