Many builders choose to make use of Map[string]interface{}. Nevertheless, after we wish to print out the results of the map, we’ll get a bunch of unreadable content material. Subsequently, we have to take away the map keywork when printing the map.
There are a number of methods to do that. A method is to make use of the fmt.Println() perform. The fmt.Println() perform will print the worth of the map, nevertheless it is not going to print the map key. One other strategy 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, nevertheless it is not going to embody 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. After we encode a map utilizing the encoding/json bundle, the map keywork shall be faraway from the ensuing JSON information.
Determine the Keywork to Take away
When printing a map utilizing the `fmt` bundle in Go, the default format consists of the sort assertion map[K]V. To take away this kind assertion, you could 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 sort assertion, you need to use the `%#v` verb as an alternative. The `#` flag suppresses the sort assertion and prints the map’s contents with out the `map[]` prefix.
Here is an instance of the right way to use the `%#v` verb to take away the sort assertion when printing a map:
“`go
bundle predominant
import “fmt”
func predominant() {
m := map[string]int{“Alice”: 20, “Bob”: 25}
fmt.Printf(“%#vn”, m)
}
“`
Output:
“`
map[Alice:20 Bob:25]
“`
As proven within the output, the sort assertion `map[]` is eliminated when utilizing the `%#v` verb.
Use the Go Print Bundle
The Go print bundle gives a number of features to format and print information. It could possibly deal with varied information varieties, together with maps. To suppress the map keywork when printing a map, you need to use the next steps:
2. Use the Fprintf Perform
The Fprintf perform takes a author (resembling os.Stdout) and a format string as its first two arguments. The format string specifies how the information needs to be formatted. To suppress the map key phrase, you need to use 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]
“`
Further Notes
Listed here are some further notes to contemplate when utilizing the Fprintf perform:
Parameter | Description |
---|---|
author | The vacation spot to which the formatted information shall be written. It may be any kind that implements the io.Author interface, resembling os.Stdout or a file. |
format string | A string that specifies how the information needs to be formatted. It could possibly include format specifiers to manage the output. |
information | The info to be formatted. It may be any kind, together with maps, slices, structs, and primitive values. |
Make the most of the PageRanges Choice
The `PageRanges` possibility in Go’s `html/pdf` bundle gives a exact resolution 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 throughout 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 gives granular management over which pages to incorporate or exclude, permitting customers to create customized printing configurations tailor-made to their particular necessities. The pliability of the `PageRanges` possibility makes it a flexible resolution for producing PDF printouts with custom-made web page choice.
Customise Web page Margins
To regulate the margins across the printed output, you may specify the Margins
subject within the PageSetup
struct. The Margins
subject 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’ll be able to modify these margins to suit your particular printing wants. For instance, if you wish to improve the highest margin to 1.5 inches, you’d set the Prime
subject 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’ll be able to arrange totally different header and footer configurations for every web page of your doc. Header and footer content material is managed independently of the prevailing structure.
Predefined Headers and Footers
There are some predefined headers and footers out there as constants:
Record 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 middle) |
html2pdf.HTopRight |
Header containing solely the web page quantity (prime proper) |
html2pdf.FDefault |
Default footer |
html2pdf.FNo |
No footer |
You’ll be able to assign one of many predefined headers or footers to a web page:
import ( "bytes" "fmt" "github.com/SebastiaanKlippert/go-wkhtmltopdf" ) func predominant() { 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 can too outline customized header or footer content material utilizing HTML:
headerContent := `My Customized Header
` web page.HeaderLeft.Contents = headerContent
Leverage the CSS Model Attribute
The CSS model attribute gives an efficient technique to hide map keywork when printing. To realize this, apply the ‘show’ property to the ‘none’ worth for the component containing the map keywork. This motion renders the component invisible through the printing course of whereas sustaining its visibility on the display screen. Implementing this resolution includes the next steps:
1. Determine the Component
Find the HTML component that encloses the map keywork. This component sometimes 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 component. Use the ‘show’ property to set the worth to ‘none’ for the ‘print’ media kind.
3. Instance
Take into account the next CSS rule:
CSS |
---|
.map-keywork { show: none; print; } |
This rule specifies that any HTML component 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. Take a look at the Answer
Preview the net web page and confirm that the map keywork is seen on the display screen. Subsequently, print the doc to verify that the keywork is hidden.
Use Exterior HTML and CSS Recordsdata
You can too 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 Disguise Components
You need to 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 might be solely helpful when viewing the map on-line. To do that, you may add the next CSS to your stylesheet:
CSS | Description |
---|---|
.element-to-hide { show: none; } |
Hides the component with the category element-to-hide |
You can too use the visibility
property to cover parts. Nevertheless, it will nonetheless render the component 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 may add the next CSS to your stylesheet:
CSS | Description |
---|---|
.element-to-hide { visibility: hidden; } |
Hides the component with the category element-to-hide |
Apply Web page Orientation Settings
To vary the orientation of your print job, observe these steps:
- From the House tab, click on the Web page Setup button.
- Within the Web page Setup dialog field, click on the Paper Dimension 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 large, and is good for printing paperwork which might be primarily text-based.
Panorama Orientation
Panorama orientation is wider than it’s tall, and is good for printing paperwork which might be primarily image-based or that require a wider structure.
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 strategy to take away them. Nevertheless, there are a couple of methods to work round this.
A method is to make use of a customized print perform. The next code defines a print perform that solely prints the values of a map:
“`go
func PrintValues(m map[string]interface{}) {
for _, v := vary m {
fmt.Println(v)
}
}
“`
This perform 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 need to use the next syntax:
“`go
for ok, v := vary m {
fmt.Println(ok, v)
}
“`
It will 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 need to use the next syntax:
“`go
for ok := vary m {
fmt.Println(ok)
}
“`
It will 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 need to use the next syntax:
“`go
for _, v := vary m {
fmt.Println(v)
}
“`
It will print the values of the map within the following format:
“`
value1
value2
“`