Go: Getting unexpected error - mysql

In my controller package, I have a AppContext struct that looks like this:
type AppContext struct {
db *sql.DB
}
func (c *AppContext) getDB() *sql.DB {
return c.db
}
Then I have the following codes in my main package:
func main {
db, err := sql.Open("mysql",
//other info)
if err != nil {
log.Fatal(err)
return
}
err = db.Ping()
if err != nil {
log.Fatal(err)
return
}
defer db.Close()
appC := controller.AppContext{db}
}
When building it, I get this unexpected error:
implicit assignment of unexported field 'db' in controller.AppContext literal
I tried looking that error up, but could not find much information on it. Is there a way to resolve this problem?

As said in the comment, db is not exported, so inaccessible from other packages.
In Go, initialization of structures is usually done with a function called NewMyStructure, so for example:
func NewAppContext(db *sql.DB) AppContext {
return AppContext{db: db}
}
and then in your main:
appC := controller.NewAppContext(db)

Related

json unmarshal not working but decode does

I have a hard time understanding why the code below, which uses the unmarshal method does not work, but then almost the same I write with NewDecoder and it works fine.
package conf
import (
"os"
"io/ioutil"
"encoding/json"
)
type Configuration struct {
Agents []Agent `json:"agents"`
IbmWmqFolder string `json:"ibmWmqFolder"`
}
type Agent struct {
AgentName string `json:"agentName"`
Folders []string `json:"folders"`
}
func LoadConfiguration() (configuration Configuration) {
jsonFile, err := os.Open("config.json")
if err != nil {
panic(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, configuration)
return
}
but if I do all the same but instead of the two last lines with the byteValue and the unmarshal itself, but use the decoder, it works,
jsonParser := json.NewDecoder(jsonFile)
jsonParser.Decode(&configuration)
return
Thanks!
I would guess that you need to pass a pointer to the configuration, like so:
json.Unmarshal(byteValue, &configuration)
You should also check the error value returned by Unmarshal, e.g.:
err = json.Unmarshal(byteValue, &configuration)
if err != nil {
panic(err)
}
See the the docs.

Printing decoded JSON in Golang

I am very new to Go / programming in general - having just picked it up whilst messing about creating my own crypto currency portfolio web site.
I am struggling printing to the web server output. If I used Printf - it prints to console but as soon as I use Fprintf to print to the web app, I get a number of errors which I can't seem to solve.
Could someone walk me through it?
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Obsidian []struct {
PriceUsd string `json:"price_usd"`
PriceBtc string `json:"price_btc"`
}
func webserver(w http.ResponseWriter, r *http.Request) {
url := "https://api.coinmarketcap.com/v1/ticker/obsidian/"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
defer resp.Body.Close()
var record Obsidian
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
fmt.Printf("%+v", record)
}
func main() {
http.HandleFunc("/test", webserver)
http.ListenAndServe(":8001", nil)
}
I have tried to replace:
fmt.Printf("%+v", record)
with:
fmt.Fprintf("%+v", record)
and receive the following errors:
./test.go:54:21: cannot use "%+v" (type string) as type io.Writer in argument to fmt.Fprintf:
string does not implement io.Writer (missing Write method)
./test.go:54:21: cannot use record (type Obsidian) as type string in argument to fmt.Fprintf
Thanks to #MiloChrisstiansen
fmt.Fprintf(w, "%+v", record)
You could also use
w.Write([]byte(record))

How to test a panic in Golang?

func good(json) string {
\\do something
err = json.Unmarshal(body, &list)
if err != nil {
panic(fmt.Sprintf("Unable to parse json %s",err))
}
}
func Testgood_PanicStatement(t *testing.T) {
Convey("And Invalid Json return error",t, func() {
actual := good("garbage json")
So(func() {},shoulPanic)
So(actual ,ShouldEqual,"")
}
}
Outcome
Line 34: - Unable to parse json ,{%!e(string=invalid character '{' looking for beginning of object key string) %!e(int64=50)}
goroutine 8 [running]:
Question:It seems like when I am passing garbage json file.It is panicking and doesn't execute any of the So statements?How to fix it?
Use recover().
func Testgood_PanicStatement(t *testing.T) {
Convey("And Invalid Json return error",t, func() {
defer func() {
if r := recover(); r != nil {
So(func() {},shouldPanic)
So(actual ,ShouldEqual,"")
}
}()
actual := good("garbage json")
}
}
Lear more about:
Golang blog
Upvoting the answer of sadlil as correct I want to point out, that panicking functions are not good practice. Rather convert the panic into an error INSIDE the function and test the error instead.
func good(json) (s string, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error in good: %v", r)
}
}()
\\do something
err = json.Unmarshal(body, &list)
if err != nil {
# leaving the panic statement for demonstration. In this case
# you could just: return "", err
panic(fmt.Sprintf("Unable to parse json %s",err))
}
return
}

Share database connection with packages

I'm new with golang. I'm trying to share mysql database connection in my package, latter maybe in several packages. To skip defining database connection in every package I've created Database package and now I'm trying to get that package, connect to db and use that object in whole package.
I'm using this mysql plugin: github.com/go-sql-driver/mysql
here is my code:
main.go
package main
import (
"log"
"./packages/db" // this is my custom database package
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
var dbType Database.DatabaseType
var db *sql.DB
func main() {
log.Printf("-- entering main...")
dbType := Database.New()
db = dbType.GetDb()
dbType.DbConnect()
delete_test_data()
dbType.DbClose()
}
func delete_test_data(){
log.Printf("-- entering delete_test_data...")
//db.Exec("DELETE FROM test;")
}
packages/db/db.go
package Database
import (
"log"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type DatabaseType struct {
DatabaseObject *sql.DB
}
func New()(d *DatabaseType) {
d = new(DatabaseType)
//db.DatabaseObject = db.DbConnect()
return d
}
func (d *DatabaseType) DbConnect() *DatabaseType{
log.Printf("-- entering DbConnect...")
var err error
if d.DatabaseObject == nil {
log.Printf("--------- > Database IS NIL...")
d.DatabaseObject, err = sql.Open("mysql", "...")
if err != nil {
panic(err.Error())
}
err = d.DatabaseObject.Ping()
if err != nil {
panic(err.Error())
}
}
return d
}
func (d *DatabaseType) DbClose(){
log.Printf("-- entering DbClose...")
defer d.DatabaseObject.Close()
}
func (d *DatabaseType) GetDb() *sql.DB{
return d.DatabaseObject
}
Everything is ok and without error until I uncomment this line:
db.Exec("DELETE FROM test;")
Can someone tell me what is correct way to share db connection?
Your dbType.DbConnect() method returns a DatabaseType with an initialized connection, but you're ignoring the return value entirely.
Further - to simplify your code - look at having New(host string) *DB instead of three different functions (New/DbConnect/GetDb) that do the same thing.
e.g.
package datastore
type DB struct {
// Directly embed this
*sql.DB
}
func NewDB(host string) (*DB, error) {
db, err := sql.Open(...)
if err != nil {
return nil, err
}
return &DB{db}, nil
}
package main
var db *datastore.DB
func main() {
var err error
db, err = datastore.NewDB(host)
if err != nil {
log.Fatal(err)
}
err := someFunc()
}
func someFunc() error {
rows, err := db.Exec("DELETE FROM ...")
// Handle the error, parse the result, etc.
}
This reduces the juggling you have to do, and you can still call close on your DB type because it embeds *sql.DB - there's no need to implement your own Close() method.

How to write a Go function to accept different structs?

I am writing a function that parses a config JSON file and using json.Unmarshal stores its data in a struct. I've done some research and it's gotten me the point where I have a Config struct and a Server_Config struct as a field in config to allow me to add more fields as I want different config-like structs.
How can I write one parseJSON function to work for different types of structs?
Code:
Server.go
type Server_Config struct {
html_templates string
}
type Config struct {
Server_Config
}
func main() {
config := Config{}
ParseJSON("server_config.json", &config)
fmt.Printf("%T\n", config.html_templates)
fmt.Printf(config.html_templates)
}
config.go
package main
import(
"encoding/json"
"io/ioutil"
"log"
)
func ParseJSON(file string, config Config) {
configFile, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(configFile, &config)
if err != nil {
log.Fatal(err)
}
}
Or if there is a better way to do all this let me know that as well. Pretty new to Go and I have Java conventions carved into my brain.
Use interface{}:
func ParseJSON(file string, val interface{}) {
configFile, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(configFile, val)
if err != nil {
log.Fatal(err)
}
}
Calling the function is the same.