Many builders want to make use of Map[string]interface{}. Nonetheless, after we wish to print out the results of the map, we are going to get a bunch of unreadable content material. Due to this fact, we have to take away the map keywork when printing the map.
There are a number of methods to do that. A technique is to make use of the fmt.Println() operate. The fmt.Println() operate will print the worth of the map, but it surely won’t print the map key. One other technique to take away the map keywork is to make use of the replicate.Worth.String() technique. The replicate.Worth.String() technique will return a string illustration of the worth of the map, but it surely won’t embrace the map key.
Lastly, we will additionally use the encoding/json bundle to take away the map keywork. The encoding/json bundle can be utilized to encode and decode JSON information. Once we encode a map utilizing the encoding/json bundle, the map keywork shall be faraway from the ensuing JSON information.
Establish the Keywork to Take away
When printing a map utilizing the `fmt` bundle in Go, the default format consists of the kind assertion map[K]V. To take away this sort assertion, you might want to specify a customized format string.
The format string is a sequence of verbs that decide how the worth is formatted. For maps, the `%v` verb is used to print the map’s contents. To take away the kind assertion, you should utilize the `%#v` verb as an alternative. The `#` flag suppresses the kind assertion and prints the map’s contents with out the `map[]` prefix.
This is an instance of methods to use the `%#v` verb to take away the kind assertion when printing a map:
“`go
bundle important
import “fmt”
func important() {
m := map[string]int{“Alice”: 20, “Bob”: 25}
fmt.Printf(“%#vn”, m)
}
“`
Output:
“`
map[Alice:20 Bob:25]
“`
As proven within the output, the kind assertion `map[]` is eliminated when utilizing the `%#v` verb.
Use the Go Print Package deal
The Go print bundle supplies a number of features to format and print information. It could deal with numerous information sorts, together with maps. To suppress the map keywork when printing a map, you should utilize the next steps:
2. Use the Fprintf Operate
The Fprintf operate takes a author (akin to os.Stdout) and a format string as its first two arguments. The format string specifies how the info must be formatted. To suppress the map key phrase, you should utilize the next format string:
“`go
fmt.Fprintf(os.Stdout, “%#vn”, m)
“`
The %#v specifier prints the map in a human-readable format, however with out the map key phrase. The ensuing output will appear to be this:
“`
map[key1:value1 key2:value2]
“`
Extra Notes
Listed here are some extra notes to contemplate when utilizing the Fprintf operate:
Parameter | Description |
---|---|
author | The vacation spot to which the formatted information shall be written. It may be any sort that implements the io.Author interface, akin to os.Stdout or a file. |
format string | A string that specifies how the info must be formatted. It could include format specifiers to manage the output. |
information | The info to be formatted. It may be any sort, together with maps, slices, structs, and primitive values. |
Make the most of the PageRanges Choice
The `PageRanges` choice in Go’s `html/pdf` bundle affords a exact answer to exclude particular pages from the printed output. This selection accepts a slice of web page ranges, the place every vary is outlined as a pair of integers representing the beginning and finish pages. Pages exterior the desired ranges are excluded from the PDF, whereas pages inside the ranges are included.
For instance, to exclude solely the primary web page from a 5-page doc, use the next code:
“`go
pageRanges := [][2]int{{2, 5}}
pdf.Configure(pdf.PageRanges(pageRanges))
“`
This method supplies granular management over which pages to incorporate or exclude, permitting customers to create customized printing configurations tailor-made to their particular necessities. The flexibleness of the `PageRanges` choice makes it a flexible answer for producing PDF printouts with custom-made web page choice.
Customise Web page Margins
To regulate the margins across the printed output, you’ll be able to specify the Margins
discipline within the PageSetup
struct. The Margins
discipline takes a set of dimensions, every representing a aspect of the web page. The margins are laid out in inches, with the next default values:
Aspect | Default Margin (inches) |
---|---|
Prime | 1 |
Proper | 1 |
Backside | 1 |
Left | 1 |
You possibly can regulate these margins to suit your particular printing wants. For instance, if you wish to enhance the highest margin to 1.5 inches, you’ll set the Prime
discipline within the Margins
struct to 1.5.
Right here is an instance of setting customized web page margins:
func examplePrintCustomMargins(w io.Author) error { doc := goxl.NewDocument("My Doc") web page := doc.AddPage() web page.SetPageSetup(&goxl.PageSetup{ Margins: &goxl.Margins{ Prime: 1.5, Proper: 0.5, Backside: 1, Left: 0.5, }, }) // Proceed including content material to the web page... if _, err := doc.Write(w); err != nil { return err } return nil }
Manipulate Header and Footer Content material
You possibly can arrange totally different header and footer configurations for every web page of your doc. Header and footer content material is managed independently of the present format.
Predefined Headers and Footers
There are some predefined headers and footers accessible as constants:
Listing of Predefined Headers and Footers
Description | |
---|---|
html2pdf.HDefault |
Default header |
html2pdf.HNo |
No header |
html2pdf.HTopLeft |
Header containing solely the web page quantity (prime left) |
html2pdf.HTopCenter |
Header containing solely the web page quantity (prime heart) |
html2pdf.HTopRight |
Header containing solely the web page quantity (prime proper) |
html2pdf.FDefault |
Default footer |
html2pdf.FNo |
No footer |
You possibly can assign one of many predefined headers or footers to a web page:
import ( "bytes" "fmt" "github.com/SebastiaanKlippert/go-wkhtmltopdf" ) func important() { pdfg, err := wkhtmltopdf.NewPDFGenerator() if err != nil { fmt.Println("Error:", err) return } web page := wkhtmltopdf.NewPage("https://github.com/SebastiaanKlippert/go-wkhtmltopdf") web page.FooterRight.Contents = wkhtmltopdf.FDefault pdfg.AddPage(web page) pdfg.PageSize.Set("A4") pdfBytes, err := pdfg.CreatePDF() if err != nil { fmt.Println("Error:", err) return } _ = bytes.NewReader(pdfBytes) }
You too can outline customized header or footer content material utilizing HTML:
headerContent := `My Customized Header
` web page.HeaderLeft.Contents = headerContent
Leverage the CSS Fashion Attribute
The CSS model attribute supplies an efficient technique to hide map keywork when printing. To realize this, apply the ‘show’ property to the ‘none’ worth for the factor containing the map keywork. This motion renders the factor invisible in the course of the printing course of whereas sustaining its visibility on the display screen. Implementing this answer entails the next steps:
1. Establish the Ingredient
Find the HTML factor that encloses the map keywork. This factor usually possesses a category or ID attribute that distinguishes it from different web page parts.
2. Outline the CSS Rule
Within the CSS stylesheet, create a rule that targets the recognized factor. Use the ‘show’ property to set the worth to ‘none’ for the ‘print’ media sort.
3. Instance
Contemplate the next CSS rule:
CSS |
---|
.map-keywork { show: none; print; } |
This rule specifies that any HTML factor with the category ‘map-keywork’ shall be hidden when the doc is printed.
4. Apply the CSS Rule
Be certain that the CSS rule is utilized to the net web page. This may be achieved by linking an exterior stylesheet or embedding the CSS guidelines immediately into the HTML doc.
5. Check the Answer
Preview the net web page and confirm that the map keywork is seen on the display screen. Subsequently, print the doc to substantiate that the keywork is hidden.
Use Exterior HTML and CSS Information
You too can use exterior HTML and CSS information to outline the styling of your printed doc. This may be helpful if you wish to use a shared template or model throughout a number of print jobs.
9. Utilizing CSS to Cover Components
You should use CSS to cover parts that you do not wish to seem on the printed web page. For instance, you may wish to disguise the map legend or different parts which are solely helpful when viewing the map on-line. To do that, you’ll be able to add the next CSS to your stylesheet:
CSS | Description |
---|---|
.element-to-hide { show: none; } |
Hides the factor with the category element-to-hide |
You too can use the visibility
property to cover parts. Nonetheless, it will nonetheless render the factor on the web page, so it is probably not the most suitable choice if you wish to save ink and paper.
To cover parts utilizing the visibility
property, you’ll be able to add the next CSS to your stylesheet:
CSS | Description |
---|---|
.element-to-hide { visibility: hidden; } |
Hides the factor with the category element-to-hide |
Apply Web page Orientation Settings
To alter the orientation of your print job, comply with these steps:
- From the Residence tab, click on the Web page Setup button.
- Within the Web page Setup dialog field, click on the Paper Measurement tab.
- Within the Orientation part, choose the specified orientation.
- Click on OK to avoid wasting your adjustments.
Portrait Orientation
Portrait orientation is the default orientation for many printers. It’s taller than it’s broad, and is good for printing paperwork which are primarily text-based.
Panorama Orientation
Panorama orientation is wider than it’s tall, and is good for printing paperwork which are primarily image-based or that require a wider format.
Orientation | Advisable for |
---|---|
Portrait | Textual content-based paperwork, letters, résumés |
Panorama | Picture-based paperwork, spreadsheets, displays |
How To Take away Map Kepwork When Printing Golang
Map keys are printed in Go by default, and there’s no built-in technique to take away them. Nonetheless, there are a couple of methods to work round this.
A technique is to make use of a customized print operate. The next code defines a print operate that solely prints the values of a map:
“`go
func PrintValues(m map[string]interface{}) {
for _, v := vary m {
fmt.Println(v)
}
}
“`
This operate can be utilized to print a map with out its keys:
“`go
m := map[string]interface{}{
“title”: “John Doe”,
“age”: 30,
}
PrintValues(m)
“`
Output:
“`
John Doe
30
“`
Individuals Additionally Ask
How To Print Map Keys And Values In Golang?
To print map keys and values, you should utilize the next syntax:
“`go
for ok, v := vary m {
fmt.Println(ok, v)
}
“`
This can print the keys and values of the map within the following format:
“`
key1 value1
key2 value2
“`
How To Print Solely Keys Of A Map In Golang?
To print solely the keys of a map, you should utilize the next syntax:
“`go
for ok := vary m {
fmt.Println(ok)
}
“`
This can print the keys of the map within the following format:
“`
key1
key2
“`
How To Print Solely Values Of A Map In Golang?
To print solely the values of a map, you should utilize the next syntax:
“`go
for _, v := vary m {
fmt.Println(v)
}
“`
This can print the values of the map within the following format:
“`
value1
value2
“`