 
  		        Siarhei
Powerful Command-Line Applications in Go: TestParseContent & TestRun failed (pages 53 - 54)
Tests on compare 2 slice of byte fail because there is a difference in the formatting we get after the parseContent() and goldenFile functions. The difference lies in different tabulation.
To bring them to a common state I suggest using the package go get -u github.com/yosssi/gohtml
func formatHTML(data []byte) []byte {
	// Convert to string for manipulation
	content := string(data)
	formattedHTML := gohtml.Format(content)
	// Return the formatted html as []byte
	return []byte(formattedHTML)
}
This formatHTML function will take raw HTML in a []byte format, format it using gohtml, and return the formatted HTML as []byte. Here’s a quick breakdown of how it works:
Conversion to String: string(data) converts the byte slice to a string, as gohtml.Format requires a string input.
Formatting*: gohtml.Format(content) processes the HTML string, adding indentation and spacing for readability.
Returning as []byte: []byte(formattedHTML) converts the formatted string back to []byte.
if !bytes.Equal(formatHTML(expected), formatHTML(result)) {
    t.Logf("golden:\n%s\n", formatHTML(expected))
    t.Logf("result:\n%s\n", formatHTML(result))
    t.Error("Result content doesn't match golden file")
}
First Post!
Popular Pragmatic Bookshelf topics
                         
                      
                       
           
           
           
           
           
           
           
           
           
          Other popular topics
                         
                      
                       
           
           
           
           
           
           
           
           
          Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /rails
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /haskell
- /emacs
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /zig
- /deepseek
- /scala
- /sublime-text
- /lisp
- /textmate
- /react-native
- /nixos
- /debian
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /deno
- /ubuntu
- /nodejs
- /spring
- /manjaro
- /diversity
- /lua
- /julia
- /slackware
- /c

 
		         
    





