felix siong
felix siong

Reputation: 37

unexpected type, expecting name

i have try to use golang. iam new for this golang. when i try to connect to mysql i got some error like this :

./testCOnnection.go:9:35: syntax error: unexpected type, expecting name ./testCOnnection.go:15:1: syntax error: non-declaration statement outside function body ./testCOnnection.go:36:5: syntax error: non-declaration statement outside function body

i have run using root.

here my source code

package main

import "database/sql"
import "fmt"
import _"github.com/go-sql-driver/mysql"

type trade_history struct {
    id, final_meta_report_id, trading_account_id, lp, lp2, lp3             int
    symbol, price, price_type, time, type, status, created_at, updated_at  string
    qty, pegged_distance, price_limit                      double
}
var db *sql.DB
var err error

funct getTradingHistory (final_Meta_report_ID int) (err error){
    username := "lalala"
    password := "felixsiong"
    host := "trades.xawrs2.us-east-2.rds.amazonaws.com"
    port := 3306
    db := "trading_dashboard"

    //use Sprintf if you wanna parse a string with values
    conn := fmt.Sprintf("%s:%s@%s:%s/%s", username, password, host, port, db)
    db, err = sql.Open("mysql", conn)
    defer db.Close()
    if err != nil {
        fmt.Println(err.Error())
    }
    err = db.Ping()
    if err != nil {
        fmt.Println(err.Error())
    }
    var p trade_history

    //if you have multiple lines of texts use ` instead of "
    err = db.QueryRow(`select id, final_meta_report_id, 
    trading_account_id, symbol, qty, price, price_type, time, lp, 
    lp2, lp3, pegged_distance, price_limit, time_limit, type, status, created_at`).Scan(&p)
    if err != nil {
        fmt.Println(err.Error())
    }


    fmt.Printf("id: %d\n Final_meta_report_id: %d\n trading_account_id: %d\n symbol: %s\n qty: %.2f\n price: %s\n price_type: %s\n time: %s\n lp: %d\n lp2: %d\n lp3: %d\n pegged_distance: %.2f\n")

    return err
}

func main() {
    getTradingHistory(2074)
}

i have update my code become this

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/go-sql-driver/mysql"
)

var db *sql.DB
var err error

type trade_history struct {
    id, final_meta_report_id, trading_account_id, lp, lp2, lp3             int
    symbol, price, price_type, time, types, status, created_at, updated_at string
    qty, pegged_distance, price_limit                                      float64
}

func getTradingHistory(final_Meta_report_ID int) error {
    username := "lalala"
        password := "felixsiong"
        host := "trades.xawrs2.us-east-2.rds.amazonaws.com"
        port := 3306
        db := "trading_dashboard"

    //use Sprintf if you wanna parse a string with values
    conn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", username, password, host, port, db)
    //conn := fmt.Sprintf("%s:%s@%s:%s/%s", username, password, host, port, db)
    db, err = sql.Open("mysql", conn)
    defer db.Close()
    if err != nil {
        fmt.Println(err.Error())
    }
    err = db.Ping()
    if err != nil {
        fmt.Println(err.Error())
    }
    var p trade_history

    //if you have multiple lines of texts use ` instead of "
    err := db.QueryRow("select id, final_meta_report_id, 
    trading_account_id, symbol, qty, price, price_type, time, lp, 
    lp2, lp3, pegged_distance, price_limit, time_limit, type, status, created_at From trade_history Where final_Meta_report_ID = ?", final_Meta_report_ID ).Scan(&p)
    if err != nil {
        fmt.Println(err.Error())
    }

    fmt.Printf("id: %d\n Final_meta_report_id: %d\n trading_account_id: %d\n symbol: %s\n qty: %.2f\n price: %s\n price_type: %s\n time: %s\n lp: %d\n lp2: %d\n lp3: %d\n pegged_distance: %.2f\n")

    return err
}

func main() {
    getTradingHistory(2074)
}

but i still got error like this if i run using terminal

./testCOnnection.go:41:55: newline in string
./testCOnnection.go:41:55: syntax error: unexpected newline, expecting comma or )
./testCOnnection.go:43:57: syntax error: unexpected type at end of statement
./testCOnnec

tion.go:43:109: invalid character U+003F '?' ./testCOnnection.go:43:144: newline in string

and if i run using visual studio code i got error like this

myConnectionMySql.go:5:8: cannot find package "github.com/go-sql-driver/mysql" in any of: /usr/local/go/src/github.com/go-sql-driver/mysql (from $GOROOT) /Users/fuad/go/src/github.com/go-sql-driver/mysql (from $GOPATH) exit status 1

Process exiting with code: 1my question is how to fix my problem here ?

Upvotes: 0

Views: 1887

Answers (4)

user12687870
user12687870

Reputation:

change this:

funct getTradingHistory (final_Meta_report_ID int) (err error){}

to:

func getTradingHistory (final_Meta_report_ID int) (err error){}

Upvotes: 0

Chamindu
Chamindu

Reputation: 183

funct getTradingHistory

Should be changed as

func getTradingHistory 

Upvotes: 0

I have rewritten your code:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
)

var myDB *sql.DB
var err error

type trade_history struct {
    id, final_meta_report_id, trading_account_id, lp, lp2, lp3             int
    symbol, price, price_type, time, types, status, created_at, updated_at string
    qty, pegged_distance, price_limit                                      float64
}

func getTradingHistory(final_Meta_report_ID int) error {
    username := "lalala"
    password := "felixsiong"
    host := "trades.xawrs2.us-east-2.rds.amazonaws.com"
    port := 3306
    db := "trading_dashboard"

    //use Sprintf if you wanna parse a string with values
    conn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", username, password, host, port, db)
    myDB, err = sql.Open("mysql", conn)
    defer myDB.Close()
    if err != nil {
        fmt.Println(err.Error())
    }
    err = myDB.Ping()
    if err != nil {
        fmt.Println(err.Error())
    }

    var p trade_history

    //if you have multiple lines of texts use ` instead of "
    err = myDB.QueryRow(`select id, final_meta_report_id,
    trading_account_id, symbol, qty, price, price_type, time, lp,
    lp2, lp3, pegged_distance, price_limit, time_limit, type, status, created_at`).Scan(&p)
    if err != nil {
        fmt.Println(err.Error())
    }

    fmt.Println("id: %d\n Final_meta_report_id: %d\n trading_account_id: %d\n symbol: %s\n qty: %.2f\n price: %s\n price_type: %s\n time: %s\n lp: %d\n lp2: %d\n lp3: %d\n pegged_distance: %.2f\n")

    return err
}

func main() {
    if getTradingHistory(2074) != nil {
        fmt.Println(err.Error())
    }
}

There is a lots of problem:

  • type is keyword for go
  • funct must be func
  • myDB.QueryRow(....).Scan(&p) need to rewrite it. you can find proper example form here
  • fmt.Println("id: %d\n Final_meta_report_id:....... need to rewrite it in right way for go
  • double will be replaced by float32/float64

Upvotes: 2

pgollangi
pgollangi

Reputation: 938

The error syntax error: unexpected type, expecting name is about the usage of reserved keyword type as variable name at specified line number. Keyword type is there to create new types in golang, cannot use it as variable name.

And there is type while declaring the function. It supposed to be func not funct. Remaining two errors are about this.

Upvotes: 1

Related Questions