Go + ichartjs + jsonp implement data chart analysis background

Source: Internet
Author: User
Tags ichart

1. Go development tool-liteide (installation instructions)

2. Go environment configuration in Windows

Download the installation package and configure environment variables.

3. Go environment configuration in Linux

Assume that it is already in the go installation directory $ go_install_dir.
# Hg clone-u release https://code.google.com/p/go
# CD go/src
#./All. Bash
The installation is successful only when "all tests passed" is displayed after all. Bash is run. The above is a UNIX-style command. The installation method in Windows is similar, except that all. bat is run, and the calling compiler is the GCC of mingw.
Then, set several environment variables,
# Export goroot = $ home/go
# Export Gobin = $ goroot/bin
# Export Path = $ path: $ Gobin

Note: You can use $ echo $ Gobin to check the environment variable settings. If these three items are not set, a problem occurs when you reference a third-party package. For example: unknown driver "MySQL" (forgotten import ?)

Installation instructions

Okay, paste the Code:

Data. Go

Purpose: create a web service and provide data interfaces.

Knowledge Point: create a web service, go data object, jsonp return data, go calls MySQL database

// Testpackage mainimport ("database/SQL" "encoding/JSON" "FMT" _ "MySQL" "net/HTTP") type drawdata struct {name string 'json: "name" 'value [] int 'json: "value" 'color string' JSON: "color" '} type alldrawdata struct {Label [] string 'json: "labels" 'drawdatas [] drawdata' JSON: "drawdata" '} func getdata (w http. responsewriter, req * HTTP. request) {req. parseform () // resolution parameter. By default, VAR jsonpcallback string = req is not parsed. form ["J Sonpcallback "] [0] datas: = [] drawdata {} alldrawdata: = alldrawdata {} dB, err: = SQL. open ("MySQL", "username: password @ TCP (127.0.0.1: 3306)/test? Charset = utf8 ") Defer dB. Close () rows0, err: = dB. Query (" select a. Username from test ") if Err! = Nil {panic (ERR)} labels: = [] string {} For rows0.next () {VaR value stringerr: = rows0.scan (& Value) If nil! = Err {panic (ERR)} labels = append (labels, value)} rows1, err: = dB. Query ("select CNT from test") if Err! = Nil {panic (ERR)} defer rows1.close () data1: = drawdata {} values: = [] int {} For rows1.next () {VaR value interr: = rows1.scan (& Value) If nil! = Err {panic (ERR)} values = append (values, value)} data1.name = "unfinished" data1.value = valuesdata1.color = "# ecad55" datas = append (datas, data1) alldrawdata. label = labelsalldrawdata. drawdatas = datasb, _: = JSON. marshal (alldrawdata) FMT. fprintf (W, jsonpcallback + "(" + String (B) + ")")} func main () {HTTP. handlefunc ("/getdatas", getdata) HTTP. listenandserve (": 1234", nil) FMT. println ("succeed! ")}

Note: you can install MySQL on the go website or download the MySQL package compiled by your predecessors and decompress it to the src/PKG directory in the go directory.

HTML page

<! Doctype HTML> 

The final result is as follows:

Ichartjs

Related Article

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.