Example of Hello World using Go language
Contents
[
Hide
]
A “Hello World” example is traditionally used to introduce features of a programming language or software with a simple use case.
Aspose.PDF for Go is a feature-rich PDF API that allows developers to embed PDF document creation, manipulation, and conversion capabilities with Go. It supports many popular file formats, including PDF, TXT, XPS, EPUB, TEX, DOC, DOCX, PPTX, image formats etc. In this article, we are creating a PDF document containing the text “Hello World!” After installing Aspose.PDF for Go in your environment, you can execute the code sample to see how the Aspose.PDF API works. Below code snippet follows these steps:
- Create a new PDF document instance.
- Add a new page to the PDF document using PageAdd function.
- Set the page size using PageSetSize.
- Add “Hello World!” text to the first page using PageAddText.
- Save the repaired PDF document using SaveAs method.
- Close the PDF document and release any allocated resources.
Following code snippet is a Hello World program to exhibit working of Aspose.PDF for Go API.
package main
import "github.com/aspose-pdf/aspose-pdf-go-cpp"
import "log"
func main() {
// Create new PDF-document
pdf, err := asposepdf.New()
if err != nil {
log.Fatal(err)
}
// Add new page
err = pdf.PageAdd()
if err != nil {
log.Fatal(err)
}
// Set page size A4
err = pdf.PageSetSize(1, asposepdf.PageSizeA4)
if err != nil {
log.Fatal(err)
}
// Add text on first page
err = pdf.PageAddText(1, "Hello World!")
if err != nil {
log.Fatal(err)
}
// Save PDF-document with "hello.pdf" name
err = pdf.SaveAs("hello.pdf")
if err != nil {
log.Fatal(err)
}
// Release allocated resources
defer pdf.Close()
}