I want to connect a MSSQL database, in python I get connecting to this database but in Go I can not.
When I exec this code I receive this error:
2017/08/04 12:25:39 Select failed:Login error: EOF
why?
Code:
package main
import (
"database/sql"
"flag"
"fmt"
"log"
_ "github.com/denisenkom/go-mssqldb"
)
var (
debug = flag.Bool("debug", false, "enable debugging")
password = flag.String("password", "********", "the database password")
port *int = flag.Int("port", 1433, "the database port")
server = flag.String("server", "sql2008v\\2k8", "the database server")
user = flag.String("user", "usuario", "the database user")
)
func main() {
flag.Parse()
if *debug {
fmt.Printf(" password:%s\n", *password)
fmt.Printf(" port:%d\n", *port)
fmt.Printf(" server:%s\n", *server)
fmt.Printf(" user:%s\n", *user)
}
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", *server, *user, *password, *port)
if *debug {
fmt.Printf(" connString:%s\n", connString)
}
conn, err := sql.Open("mssql", connString)
if err != nil {
log.Fatal("Open connection failed:", err.Error())
}
defer conn.Close()
rows, err := conn.Query("SELECT TOP (1000) * FROM [jiradb].[jiraschema].[jiraissue]")
if err != nil {
log.Fatal("Select failed:", err.Error())
}
defer rows.Close()
fmt.Printf("bye\n")
}
Related
I have a text file with this content:
192.168.1.2$nick
192.168.1.3$peter
192.168.1.4$mike
192.168.1.5$joe
A web server is running on each IP in the list.
I need to check if the servers are currently available and output a message if not available.
I wrote a small application. It works, but periodically produces incorrect results - it does not output messages for servers that are not actually available.
I can't figure out what's going on and in fact I'm not sure if I'm using http.Client correctly in goroutines.
Help me plese.
package main
import "fmt"
import "os"
import "strings"
import "io/ioutil"
import "net/http"
import "crypto/tls"
import "time"
import "strconv"
func makeGetRequest(URL string, c *http.Client) {
resp, err := c.Get(URL)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
if !((resp.StatusCode >= 200 && resp.StatusCode <= 209)) {
fmt.Printf("%s-%d\n", URL, resp.StatusCode)
}
}
func makeHeadRequestAsync(tasks chan string, done chan bool, c *http.Client) {
for {
URL := <-tasks
if len(URL) == 0 {
break
}
resp, err := c.Head(URL)
if err != nil {
fmt.Println(err)
continue
}
defer resp.Body.Close()
if !((resp.StatusCode >= 200 && resp.StatusCode <= 209)) {
makeGetRequest(URL, c) // !!! Some servers do not support HEAD requests. !!!
}
}
done <- true
}
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: main <number of threads> <input-file>")
os.Exit(0)
}
threadsNum, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println("Bad first parameter. Exit.")
os.Exit(0)
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := &http.Client {
Timeout: 30 * time.Second,
}
file, err := ioutil.ReadFile(os.Args[2])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fileLines := strings.Split(string(file), "\n")
tasks := make(chan string, threadsNum)
done := make(chan bool)
for i := 0; i < threadsNum; i++ {
go makeHeadRequestAsync(tasks, done, client)
}
for i := 0; i < len(fileLines); i++ {
tasks <- strings.Split(string(fileLines[i]), "$")[0:1][0]
}
for i := 0; i < threadsNum; i++ {
tasks <- ""
<-done
}
}
The program terminates when the main() function returns. The code does not ensure that all goroutines are done before returning from main.
Fix by doing the following:
Use a sync.WaitGroup to wait for the goroutines to complete before exiting the program.
Exit the goroutine when tasks is closed. Close tasks after submitting all work.
Here's the code:
func makeHeadRequestAsync(tasks chan string, wg *sync.WaitGroup, c *http.Client) {
defer wg.Done()
// for range on channel breaks when the channel is closed.
for URL := range tasks {
resp, err := c.Head(URL)
if err != nil {
fmt.Println(err)
continue
}
defer resp.Body.Close()
if !(resp.StatusCode >= 200 && resp.StatusCode <= 209) {
makeGetRequest(URL, c) // !!! Some servers do not support HEAD requests. !!!
}
}
}
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: main <number of threads> <input-file>")
os.Exit(0)
}
threadsNum, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println("Bad first parameter. Exit.")
os.Exit(0)
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := &http.Client{
Timeout: 30 * time.Second,
}
file, err := ioutil.ReadFile(os.Args[2])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fileLines := strings.Split(string(file), "\n")
tasks := make(chan string)
var wg sync.WaitGroup
wg.Add(threadsNum)
for i := 0; i < threadsNum; i++ {
go makeHeadRequestAsync(tasks, &wg, client)
}
for i := 0; i < len(fileLines); i++ {
tasks <- strings.Split(string(fileLines[i]), "$")[0:1][0]
}
close(tasks)
wg.Wait()
}
My java code below:
public static byte[] gzip(String str) throws Exception{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(str.getBytes("UTF-8"));
gos.close();
return baos.toByteArray();
}
How to gzip string and return byte array in golang as my java done?
Here is complete example of gzipString function which uses standard library compress/gzip
package main
import (
"bytes"
"compress/gzip"
"fmt"
)
func gzipString(src string) ([]byte, error) {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
_, err := zw.Write([]byte(src))
if err != nil {
return nil, err
}
if err := zw.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func main() {
gzippedBytes, err := gzipString("")
if err != nil {
panic(err)
}
fmt.Printf("Zipped out: %v", gzippedBytes)
}
Have a look at following snippet. Playgorund: https://play.golang.org/p/3kXBmQ-c9xE
Golang has everything in its standard library. Check https://golang.org/pkg/compress/gzip
package main
import (
"bytes"
"compress/gzip"
"fmt"
"log"
"strings"
"io"
)
func main() {
s := "Hello, playground"
// Create source reader
src := strings.NewReader(s)
buf := bytes.NewBuffer(nil)
// Create destination writer
dst := gzip.NewWriter(buf)
// copy the content as gzip compressed
_, err := io.Copy(dst, src)
if err != nil {
log.Fatal(err)
}
fmt.Println(buf.String())
}
This is my first Go program. I'm learning the language but it's a bit difficult to understand all the concepts so in order to practice I wrote a code to detect same file. It's a simple program which recursively check for duplicated files in a directory.
but:
how to detect duplicate file in directory files
the matter isn't directory recursively. the matter is how to compare
You could take the hash of each file body and then compare the hashes in a dictionary/map.
package main
import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"log"
"os"
)
func main() {
contentHashes := make(map[string]string)
if err := readDir("./", contentHashes); err != nil {
log.Fatal(err)
}
}
func readDir(dirName string, contentHashes map[string]string) (err error) {
filesInfos, err := ioutil.ReadDir(dirName)
if err != nil {
return
}
for _, fi := range filesInfos {
if fi.IsDir() {
err := readDir(dirName+fi.Name()+"/", contentHashes)
if err != nil {
return err
}
} else {
// The important bits for this question
location := dirName + fi.Name()
// open the file
f, err := os.Open(location)
if err != nil {
return err
}
h := md5.New()
// copy the file body into the hash function
if _, err := io.Copy(h, f); err != nil {
return err
}
// Check if a file body with the same hash already exists
key := fmt.Sprintf("%x", h.Sum(nil))
if val, exists := contentHashes[key]; exists {
fmt.Println("Duplicate found", val, location)
} else {
contentHashes[key] = location
}
}
}
return
}
use sha256 to compare files
example:
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"sync"
"flag"
"runtime"
"io"
)
var dir string
var workers int
type Result struct {
file string
sha256 [32]byte
}
func worker(input chan string, results chan<- *Result, wg *sync.WaitGroup) {
for file := range input {
var h = sha256.New()
var sum [32]byte
f, err := os.Open(file)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
if _, err = io.Copy(h, f); err != nil {
fmt.Fprintln(os.Stderr, err)
f.Close()
continue
}
f.Close()
copy(sum[:], h.Sum(nil))
results <- &Result{
file: file,
sha256: sum,
}
}
wg.Done()
}
func search(input chan string) {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else if info.Mode().IsRegular() {
input <- path
}
return nil
})
close(input)
}
func main() {
flag.StringVar(&dir, "dir", ".", "directory to search")
flag.IntVar(&workers, "workers", runtime.NumCPU(), "number of workers")
flag.Parse()
fmt.Printf("Searching in %s using %d workers...\n", dir, workers)
input := make(chan string)
results := make(chan *Result)
wg := sync.WaitGroup{}
wg.Add(workers)
for i := 0; i < workers; i++ {
go worker(input, results, &wg)
}
go search(input)
go func() {
wg.Wait()
close(results)
}()
counter := make(map[[32]byte][]string)
for result := range results {
counter[result.sha256] = append(counter[result.sha256], result.file)
}
for sha, files := range counter {
if len(files) > 1 {
fmt.Printf("Found %d duplicates for %s: \n", len(files), hex.EncodeToString(sha[:]))
for _, f := range files {
fmt.Println("-> ", f)
}
}
}
}
Hi I Made a simple photo blog app with go and when I deployed it to App Engine it had issues updating the pictures on the home page. When i upload a picture and go back to the home page, it take a few refreshes to show the image. https://hello-world-170523.appspot.com/.
login info:
email: test#example.com
password: test
This is the source code:
package main
import (
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"google.golang.org/appengine"
"github.com/gorilla/sessions"
)
type IndexPage struct {
Photos []string
LogedIn bool
}
type LoginPage struct {
Body string
FirstName string
LastName string
Email string
Error string
}
type UploadPage struct {
Error string
Msg string
}
func getPhotos() []string {
photos := make([]string, 0)
filepath.Walk("assets/img", func(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
path = strings.Replace(path, "\\", "/", -1)
photos = append(photos, path)
return nil
})
return photos
}
var store = sessions.NewCookieStore([]byte("HelloWorld"))
func loginPage(res http.ResponseWriter, req *http.Request) {
loginError := ""
session, _ := store.Get(req, "session")
str, _ := session.Values["logged-in"].(string)
if str == "YES" {
http.Redirect(res, req, "/admin", 302)
return
}
if req.Method == "POST" {
email := req.FormValue("email")
password := req.FormValue("password")
if email == "test#example.com" && password == "test" {
session.Values["logged-in"] = "YES"
session.Save(req, res)
http.Redirect(res, req, "/admin", 302)
return
} else {
loginError = "Invalid Credential. Please Resubmit"
}
}
tpl, err := template.ParseFiles("assets/tpl/login.gohtml", "assets/tpl/header.gohtml")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
err = tpl.Execute(res, LoginPage{
Error: loginError,
})
}
func admin(res http.ResponseWriter, req *http.Request) {
uploadError := ""
successMsg := ""
session, _ := store.Get(req, "session")
str, _ := session.Values["logged-in"].(string)
if str != "YES" {
http.Redirect(res, req, "/login", 302)
return
}
if req.Method == "POST" {
// <input type="file" name="file">
src, hdr, err := req.FormFile("file")
if err != nil {
http.Error(res, "Invalid File.", 500)
return
}
defer src.Close()
// create a new file
// make sure you have a "tmp" directory in your web root
dst, err := os.Create("assets/img/" + hdr.Filename)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer dst.Close()
// copy the uploaded file into the new file
io.Copy(dst, src)
}
tpl, err := template.ParseFiles("assets/tpl/admin.gohtml", "assets/tpl/header.gohtml")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
err = tpl.Execute(res, UploadPage{
Error: uploadError,
Msg: successMsg,
})
if err != nil {
http.Error(res, err.Error(), 500)
}
}
func index(res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "session")
str, _ := session.Values["logged-in"].(string)
logged := false
if str == "YES" {
logged = true
}
tpl, err := template.ParseFiles("assets/tpl/index.gohtml", "assets/tpl/header.gohtml")
if err != nil {
fmt.Println(err)
http.Error(res, err.Error(), 500)
return
}
err = tpl.Execute(res, IndexPage{
Photos: getPhotos(),
LogedIn: logged,
})
if err != nil {
fmt.Println(err)
http.Error(res, err.Error(), 500)
}
}
func logout(res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "session")
str, _ := session.Values["logged-in"].(string)
if str == "YES" {
delete(session.Values, "logged-in")
session.Save(req, res)
http.Redirect(res, req, "/", 302)
} else {
http.Redirect(res, req, "/login", 302)
}
}
func deletePic(res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "session")
str, _ := session.Values["logged-in"].(string)
if str != "YES" {
http.Redirect(res, req, "/", 302)
return
}
if req.Method == "POST" {
imgName := req.FormValue("imgName")
err := os.Remove(imgName)
if err != nil {
http.Error(res, err.Error(), 500)
}
}
tpl, err := template.ParseFiles("assets/tpl/delete.gohtml", "assets/tpl/header.gohtml")
if err != nil {
http.Error(res, err.Error(), 500)
}
err = tpl.Execute(res, IndexPage{
Photos: getPhotos(),
})
if err != nil {
http.Error(res, err.Error(), 500)
}
}
func main() {
http.HandleFunc("/delete", deletePic)
http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets"))))
http.HandleFunc("/", index)
http.HandleFunc("/admin", admin)
http.HandleFunc("/login", loginPage)
http.HandleFunc("/logout", logout)
appengine.Main()
}
And my app.yaml:
runtime: go
env: flex
"http.Post" expects a "Reader" as the body argument. "File" implements "Reader".
But if I pass file as the body argument I always receive 0 bytes at the other end. Why?
Here is the code:
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
file, err := os.Open("lala.txt")
if err != nil {
fmt.Printf("file open errrrr %v \n", err)
}
defer file.Close()
resp, err := http.Post("http://requestb.in/11fta851", "text/plain", file)
if err != nil {
fmt.Printf("errrrr %v \n", err)
} else {
fmt.Printf("resp code %d \n", resp.StatusCode)
}
}
I know that you could do "file.ReadAll" to a buffer and use that. But it feels like double work.
The site requestb.in seems to ignore POST data if the header Content-Length is not specified. This code works:
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
file, err := os.Open("lala.txt")
if err != nil {
fmt.Printf("file open errrrr %v \n", err)
}
defer file.Close()
req, _ := http.NewRequest("POST", "http://requestb.in/1fry3jy1", file)
req.ContentLength = 5
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("errrrr %v \n", err)
} else {
fmt.Printf("resp code %d \n", resp.StatusCode)
}
}