Use go language to build a reading website similar to FM

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

This time in learning go language and Beego framework, write a simple website practice practiced hand

The function is simple, reading an article randomly from the database and presenting it to the website.

The main issues to be addressed are: 1. Read all txt files from the folder and save to MySQL

2. Randomly read article from MySQL


Traverse the folder, find all txt files and remove the suffix and path prefixes as the title of the article, then read the file, and deposit it into the database:

Inserts all specific types of files under the specified directory into the database func insertall (dir, suffix string) {var title stringdb, err: = SQL. Open ("MySQL", "Root:123@/myblog?charset=utf8") checkerr (Err) length, fname: = ReadAll (dir, suffix) I: = 1var BCT []byte// Content for {If i > length {BREAK}BCT, err = Ioutil. ReadFile (Fname[i-1]) Checkerr (err) title = Delsuffix (Delprefix (fname[i-1])) stmt, errs: = db. Prepare ("Insert blog set title=?,content=?") Checkerr (errs) _, Err2: = stmt. Exec (title, BCT) Checkerr (ERR2) i++}}//gets all file information with the suffix suffix (dir, readall string) under DIR (n int, result [suffix) { var length intlength = 0filepath. Walk (dir, func (path string, f OS). FileInfo, err Error) error {if f = = nil {return err}if F.isdir () {return nil}if istype (path, suffix) {Length++}return nil} ) Re: = make ([]string, length) length = 0filepath. Walk (dir, func (path string, f OS). FileInfo, err Error) error {if f = = nil {return err}if F.isdir () {return nil}if istype (path, suffix) {re[length] = Pathlen Gth++}return nil}) return length, Re}func istype (filename, Typ string) (re bool) {rs: = []byte (filename) RL: = Len (rs) N: = strings. LastIndex (FileName, ".") TMP: = string (RS[N+1:RL]) if tmp = = Typ {return true}return false}//remove file name before the path func Delprefix (Origin string) (re string) { RS: = []byte (origin) RL: = Len (rs) N: = strings. LastIndex (origin, "\ \") Result: = String (Rs[n+1:rl]) return result}//remove suffix func delsuffix (Origin string) (re string) {rs: = []byte (Origin) N: = strings. LastIndex (Origin, ".") Result: = String (Rs[0:n]) return Result}func Checkerr (err error) {if err! = Nil {panic (err)}}
Random Read article:

Func Getrandblog () (BL Blog) {//randomly returns a Blogvar num inti from the database: = 0r: = Rand. New (Rand. Newsource (time. Now (). Unixnano ())) db, err: = SQL. Open ("MySQL", "Root:123@/myblog?charset=utf8") Checkerr (ERR) db. Queryrow ("SELECT count (*) from blog"). Scan (&num) Arrnum: = Make ([]int, num) rows, _: = db. Query ("Select ID from the blog") for rows. Next () {err = rows. Scan (&arrnum[i]) I++}return One (ARRNUM[R.INTN (num)))}//read the text file from the database by ID func one (id int) (BL Blog) {var re blogdb, ERR: = SQL. Open ("MySQL", "Root:123@/myblog?charset=utf8") Checkerr (ERR) db. Queryrow ("Select Title,content from blog where id=?", id). Scan (&re. Title, &re. Content) return re}
Code: Https://github.com/Zuru2048/Go/tree/master/MyBlog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.