Skip to main content

Cara Membuat Aplikasi RSS dengan Golang

Pada tutorial kali ini kita akan menggunakan Golang untuk membuat RSS content dengan Golang. Kita bisa integrasikan pada aplikasi berbasis web yang kita  buat dengan Golang.

Untuk source pertama, kita buat dulu file rss feed.go dengan isi sebagai berikut:

package main


import (

"encoding/json"

"github.com/gorilla/feeds"

"io/ioutil"

"log"

"os"

"time"

)


type FeedsData struct {

ID string `json:"id"`

Title string `json:"title"`

Description string `json:"description"`

}


func generateRssFeeds() *feeds.Feed {


//Initialize RSS Feeds

feed := &feeds.Feed{

Title: "RSS Feeds Example",

Link: &feeds.Link{Href: "/rssFeedsLink"},

Description: "This is RSS Feeds Example In Go language",

Author: &feeds.Author{Name: "author"},

Created: time.Now(),

}


//Get JSON data from file, here you can retrieve data from database table also.

feedsData := getJSONFeedsDataFromFile()

var feedItems []*feeds.Item


// Add items to RSS feeds

for i := 0; i < len(feedsData); i++ {


item := feedsData[i]


feedItems = append(feedItems,

&feeds.Item{

Id: item.ID,

Title: item.Title,

Link: &feeds.Link{Href: "/rssFeedsLink"},

Description: item.Description,

Created: time.Now(),

})

}


//Append Items

feed.Items = feedItems


return feed

}


func getJSONFeedsDataFromFile() []FeedsData {


//open JSON file

jsonFile, err := os.Open("feeds.json")

if err != nil {

log.Fatal(err)

}


//defer the closing of jsonFile, we can parse it later on

defer jsonFile.Close()


//read opened jsonFile as a byte array.

byteValue, _ := ioutil.ReadAll(jsonFile)


if err != nil {

log.Fatal(err)

}

// defer the closing of our jsonFile so that we can parse it later on

defer jsonFile.Close()


var data []FeedsData


// Unmarshal byteArray into FeedsData

err = json.Unmarshal(byteValue, &data)


if err != nil {

log.Fatal(err)

}


return data

}

dan kemudian gunakan function tersebut dengan membuat file main.go:

package main


import (

"fmt"

"github.com/gorilla/feeds"

)


func main() {


//generate RSS feeds data from JSON file

feedsData := generateRssFeeds()


/*

convert it to RSS.

github.com/gorilla/feeds also provides methods for converting to RSS, JSON and Atom

*/


rssFeed := (&feeds.Rss{Feed: feedsData}).RssFeed()


// Printing response to XML

xmlRssFeeds := rssFeed.FeedXml()


fmt.Println(xmlRssFeeds)

}

dan coba.

Comments

Popular posts from this blog

How to Install Traefik in Debian (Without Docker)

 Download the suitable version of Traefik for your system from here: https://github.com/traefik/traefik/releases as an example, we'll be download the linux 64 bit version: wget https://github.com/traefik/traefik/releases/download/v2.8.7/traefik_v2.8.7_linux_amd64.tar.gz extract those package: tar -xzvf traefik_v2.8.7_linux_amd64.tar.gz set the traefik as executable and move the traefik binary to linux bin folder

Cara Disable Antimalware Service Executable di Windows 10

Disadari atau tidak, Windows 10 (dan juga windows-windows lainnya) hadir dengan banyak sekali aplikasi bloatware (aplikasi yang tidak perlu-perlu amat dimiliki oleh end user). Contohnya, adalah aplikasi yang seharusnya sudah tergantikan fungsinya oleh antivirus, seperti Antimalware Service Executable . Aplikasi ini dicurigai membuat Windows 10 mengalami inefisiensi memori/RAM, memakan resource yang tinggi, dengan Load yang tinggi (tanpa limit terkadang). Nah, berikut adalah cara men-disable nya: Tekan tombol Windows + I untuk membuka apliaksi Windows Setting. Pilih icon menu Update and Security Pilih lagi menu disamping kiri Windows Security Pada jendela baru yang muncul, ada pilihan Virus & Threat protection Klik ini Lalu matikan proses Real-time protection tersebut. Dengan Regedit. Buka dialog regedit, Windows + R dan ketik ‘regedit’ Cari Folder regedit ini HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender Buat sebuah DWORD baru dengan klik kanan

Setup Debian 11 Official Repository In sources.list (/etc/apt/sources.list)

When you install Debian 11 using a DVD, the OS installer sets the DVD as the source for getting packages for your system if you didn't choose to scan for network mirrors. Due to this reason, the system would ask you to insert a DVD if the disc is not present in the DVD drive when you try to install any software.  Also, the packages on DVD may not be the latest release.  So, we need to get the packages from the Internet. Media change: please insert the disc labeled 'Debian GNU/Linux 11.0.0 _Bullseye_ - Official amd64 DVD Binary-1 20210814-10:04' in the drive '/media/cdrom/' and press [Enter] The /etc/apt/sources.list file with DVD as a source would look something like below. # deb cdrom:[Debian GNU/Linux 11.0.0 _Bullseye_ - Official amd64 DVD Binary-1 20210814-10:04]/ bullseye contrib main deb cdrom:[Debian GNU/Linux 11.0.0 _Bullseye_ - Official amd64 DVD Binary-1 20210814-10:04]/ bullseye contrib main deb http://security.debian.org/debian-security bullseye-security