go-optics

Go-optics. Modifying immutable data types in Go. #

Go-optics provides tooling and functions to modify deeply nested immutable data structures.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
	//Create a nested immutable data structure.
	data := NewBlogPost(
		"BlogPost",
		[]Comment{
			NewComment("First Comment", "First comment content"),
			NewComment("Second Comment", "Second comment content"),
		},
	)

	//Create an optic to focus the first comments content.
	optic := O.BlogPost().Comments().Nth(0).Content()

	//Set a new value to the focused comment.
	newData := MustSet(
		optic,
		"Updated comment content",
		data,
	)

	fmt.Println(newData)
	//Output:
	//{BlogPost [{First Comment Updated comment content} {Second Comment Second comment content}] []}

The example above is editable just click on the Playground tab.

Check out the:

  • Introduction for a more detailed introduction to go-optics.
  • Concepts section for an in depth overview of the concepts in go-optics
  • Using section for an deep dive into how to use go-optics

Then explore the rest of the documentation using the links on the left.