Activity GO FINAL
Activity GO FINAL
1
Q1. Write a program for Form validation in golang.
package main
import (
"html/template"
"log"
"net/http"
"github.com/bmizerany/pat"
)
func main() {
mux := pat.New()
mux.Get("/", http.HandlerFunc(home))
mux.Post("/", http.HandlerFunc(send))
mux.Get("/confirmation", http.HandlerFunc(confirmation))
log.Print("Listening...")
err := http.ListenAndServe(":3000", mux)
if err != nil {
log.Fatal(err)
}
}
2
func send(w http.ResponseWriter, r *http.Request) {
// Step 1: Validate form
// Step 2: Send message in an email
// Step 3: Redirect to confirmation page
}
3
Output :
4
Q2. Write a program to create a photo gallery in golang.
package main
import (
"crypto/sha1"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
func init() {
tpl = template.Must(template.ParseGlob("templates/*"))
}
func main() {
http.HandleFunc("/", index)
http.Handle("/gallery/", http.StripPrefix("/gallery",
http.FileServer(http.Dir("./gallery"))))
http.Handle("/favicon.ico", http.NotFoundHandler())
http.ListenAndServe(":8080", nil)
}
5
func index(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost {
mf, fh, err := req.FormFile("nf")
if err != nil {
fmt.Println(err)
}
defer mf.Close()
// create sha for file name
ext := strings.Split(fh.Filename, ".")[1]
h := sha1.New()
io.Copy(h, mf)
fname := fmt.Sprintf("%x", h.Sum(nil)) + "." + ext
// create new file
wd, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
path := filepath.Join(wd, "gallery", fname)
nf, err := os.Create(path)
if err != nil {
fmt.Println(err)
}
defer nf.Close()
mf.Seek(0, 0)
io.Copy(nf, mf)
6
}
file, err := os.Open("gallery")
if err != nil {
log.Fatalf("failed opening directory: %s", err)
}
defer file.Close()
list, _ := file.Readdirnames(0) // 0 to read all files and folders
tpl.ExecuteTemplate(w, "index.gohtml", list)
}
Output :
7
Q3. Write a program to generate QR code in golang.
package main
import (
"fmt"
"image"
"image/png"
"os"
qrcode "github.com/skip2/go-qrcode"
)
func main() {
// Data to encode into QR code
data := "https://www.example.com"
qrCode, err := qrcode.Encode(data, qrcode.Medium, 256)
if err != nil {
fmt.Println("Error generating QR code:", err)
return
}
qrImage, err := qrCode.PNG(256)
if err != nil {
fmt.Println("Error creating QR code image:", err)
return
}
8
// Save QR code image to file
file, err := os.Create("qrcode.png")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
Output :
9
Q 4. Write a program on Concurrency in golang
package main
import (
"fmt"
"time"
)
func main() {
go helloworld()
time.Sleep(3 * time.Second)
goodbye()
}
func helloworld() {
fmt.Println("Hello World!")
}
func goodbye() {
fmt.Println("Good Bye!")
}
10
Q5. Write a program on Interface with a common method.
package main
import "fmt"
type Shape interface {
Area() float64
}
11
func main() {
rectangle := Rectangle{Width: 5, Height: 3}
circle := Circle{Radius: 2}
printArea(rectangle)
printArea(circle)
}
Output :
12
Q6 ) Write a program to get the current date and timestamp in
local and other timezones.
package main
import (
"fmt"
"time"
)
func main() {
// Get current time in local timezone
localTime := time.Now()
fmt.Println("Local Time:", localTime.Format(time.RFC3339))
13
otherTime := time.Now().In(loc)
fmt.Printf("%s Time: %s\n", otherTimezone,
otherTime.Format(time.RFC3339))
}
Output
14