Posts

Showing posts with the label go

Go - How to keep files in memory - transferring files between servers without storing them locally

Image
Clash Royale CLAN TAG #URR8PPP Go - How to keep files in memory - transferring files between servers without storing them locally I am in the process of writing a simple deployment tool which needs to take tar files from s3, extract them and then upload them to our staging server. I would like to do this without storing the files locally, keeping them in memory. Here is my code to download files from s3 func s3downloadFile(downloader *s3manager.Downloader, item string) { localitem := strings.Replace(item, s3base, "", -1) os.MkdirAll(path.Dir(localitem), os.ModePerm) file, err := os.Create(localitem) if err != nil { exitErrorf("Unable to open file %q, %v", err) } defer file.Close() numBytes, err := downloader.Download(file, &s3.GetObjectInput{ Bucket: aws.String(s3bucket), Key: aws.String(item), }) if err != nil { exitErrorf("Unable to download item %q, %v", item,...

How to run/debug a beego app using Gogland (go language)

Image
Clash Royale CLAN TAG #URR8PPP How to run/debug a beego app using Gogland (go language) Im using Gogland (IDE from JetBrains. Version 1.0 Preview/EAP Feb, 10 2017) to create a Beego web app. I can run it from command line with: bee run and everything works. However if I run it from the IDE with the following configuration when I go to localhost:8080 it says it can not find the template file in path: I thought it was related to GOPATH, but then I realized that Gogland IDE is probably running go run main.go instead of bee go and when I checked runing go run main.go from the shell, I got the same issue: cant find the template. I even tried to run the 'bee' command from the IDE. I partially succeed. With this configuration: I can run it from the IDE, but the debugger doesn't stop in any breakpoint. IE: I can only run (but not debug) it, from Gogland. So my question is how to make gogland IDE debug a beego project Maybe you can change the command w...

Parsing dates with negative year in Go?

Image
Clash Royale CLAN TAG #URR8PPP Parsing dates with negative year in Go? How do I parse the date "3 Mar -1500" to represent 1500 BC? https://play.golang.org/p/akqQPj4mLeo According to the time.Parse function documentation: Years must be in the range 0000..9999. – Ortomala Lokni 13 hours ago 1 Answer 1 Here is a first draft that illustrates the concept: package main import ( "fmt" "strings" "time" ) func parseCEDate(value string) (time.Time, error) { const layout = "_2 Jan 2006" date, err := time.Parse(layout, value) if err == nil { return date, err } perr, ok := err.(*time.ParseError) if !ok { return time.Time{}, err } if perr.LayoutElem != "2...

Golang template generate array of strings

Image
Clash Royale CLAN TAG #URR8PPP Golang template generate array of strings I want to generate the following app1 && app2 && app3 app1 && app2 && app3 My template look like following {{- range ExeApp .}} {{ .Command }} {{- end}} My code looks like following with command which is array of strings type App struct { Data string Command string } func ExeApp(m models) App { switch m.Type { case “apps": return App{ {"# running apps", string{“app1", “app2", “app3"}}, } … Currently its generated like [app1 app2 app3] And I need it like app1 && app2 && app3 , app1 && app2 && app3 I try to play with the template by adding && before the .Command which doesn’t help and in addition I don’t know how to remove the array before and after the apps, any idea what I’m doing wrong here ? By c...

How to get the size of available TCP data?

Image
Clash Royale CLAN TAG #URR8PPP How to get the size of available TCP data? Problem I have a use case where I need to Peek at exactly the first TCP packet, whatever length it may be. Peek Snippet I would have expected this to work: conn, err := sock.Accept() if nil != err { panic(err) } // plenty of time for the first packet to arrive time.Sleep(2500 * 1000000) bufConn := bufio.NewReader(conn) n := bufConn.Buffered() fmt.Fprintf(os.Stdout, "Size of Buffered Data %dn", n) However, even though I am positive that the data has arrived it still shows that 0 bytes are buffered. Full Test Application Here's a full test program: package main import ( "bufio" "fmt" "net" "os" "strconv" "time" ) func main () { addr := ":" + strconv.Itoa(4080) sock, err := net.Listen("tcp", addr) if nil != err { panic(err) } conn, err := sock.Accept() if nil != e...

How does one test net.Conn in unit tests in Golang?

Image
Clash Royale CLAN TAG #URR8PPP How does one test net.Conn in unit tests in Golang? I'm currently looking into creating some unit tests for net.Conn interface in Go, as well as other functions that build up on top of that functionality, and I'm wondering what is the best way to unit test that in Google Go? My code looks like: conn, _:=net.Dial("tcp", "127.0.0.1:8080") ... fmt.Fprintf(conn, "test") ... buffer:=make(byte, 100) conn.Read(buffer) Is the most efficient way of testing this code and the code that uses these functions to spin up a separate goroutine to act like the server, use net.http.httptest package, or something else? Suggest reading the source for the tests for the actual net library. I have picked up plenty of tips by doing that in the past. Secondly as you have already mentioned use the httptest package. – miltonb Jun 7 '15 at 4:31 ...

Get file size given file descriptor in Go

Image
Clash Royale CLAN TAG #URR8PPP Get file size given file descriptor in Go If given a path, I would use this to get file size file, _ := os.Open(path) fi, _ := file.Stat() fsuze := fi.Size() But if only given fd , how can I get the file size? Is there any way in golang like fd lseek(fd, 0, SEEK_END) in C Can I get File stat without opening the file? Just like C, given path int stat(const char*path, struct stat *statbuf) I just looked into the golang os package and found func Stat(name string) (FileInfo, error) just like C's int stat(const char*path, struct stat *statbuf) Just to clarify, does fd mean an integer file descriptor, as one would get from open() in C? – Jonathon Reinhart 41 mins ago fd open() Yes. I get this fd by calling os.File.stat().Fd() ...

Golang HTML/template return 404 file not found

Image
Clash Royale CLAN TAG #URR8PPP Golang HTML/template return 404 file not found I'm trying to render home.html on port 8080. server.go package main import ( "html/template" "net/http" ) var homeT = template.Must(template.ParseFiles("exhibit-b/home.html")) func home(w http.ResponseWriter, r *http.Request) { homeT.Execute(w, nil) } func main() { http.HandleFunc("/home", home) http.ListenAndServe(":8080", nil) } home.html <!DOCTYPE html> <html> <title>Learning Go</title> <body> <h1>Yes I understand!</h1> <h2>... and I would like to do something more interesting</h2> </body> </html> But when I run the above code go run server.go the server starts but when I go to localhost:8080 I just see 404 error go run server.go By clicking "Post Your Answer", you acknowledge that y...

How to populate and embedded array with gorm?

Image
Clash Royale CLAN TAG #URR8PPP How to populate and embedded array with gorm? I have 2 structs with data like this: type User struct { Pics Pic } type Pic struct { Id int UserId int64 } Although everytime I insert an User, Each of the pics are inserted on their table everytime I find the users, pics are not populated: var users User db.Limit(pagesize).Where("updated_at > ?", date).Find(&users) Am I doing something wrong? I have no experience with gorm, but does your query return the pic data? – Justin Wood Mar 11 '15 at 20:24 no, that's the problem, it doesn't return anything on the array – Javier Manzano Mar 12 '15 at 12:11 ...

how to get the element name of a deeper lying element in XML with Golang

how to get the element name of a deeper lying element in XML with Golang I got the following XML structure XML-structure: <assay> <step id="1"> <command> <bar> <ab>structure 1</ab> </bar> <duration>5</duration> </command> </step> <step id="2"> <command> <bar> <cd>structure 2</cd> </bar> <duration>5</duration> </command> </step> <step id="3"> <command> <bar> <de>structure 3</de> </bar> <duration>5</duration> </command> </step> <step id="4"> <command> <bar> <ab>structure 1</ab...

How to use libsodium to encrypt/decrypt data along with ed25519 for generating keys?

How to use libsodium to encrypt/decrypt data along with ed25519 for generating keys? Currently, I am investigating on libsodium for encrypt/decrypt along with ed25519 for generating keys. I had gone through google, but I am quite confused on how to implement it. Can someone give me some examples of how can I use it in go or python? By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Difference between os.File, io.Reader and os.Stdin

Difference between os.File, io.Reader and os.Stdin I was looking at NewScanner in the official go docs and it mentions the parameter to be passed to bufio.NewScanner should be of type io.Reader . However, the following works for me: bufio.NewScanner io.Reader file, err := os.Open("filename") scanner := bufio.NewScanner(file) The same can be seen for os.Stdin as well. Given this what is the difference between os.File , os.Stdin and io.Reader ? Are they interchangeable? os.Stdin os.File os.Stdin io.Reader io.Reader is an interface. os.File and os.Stdin implement this interface having method Read() and can be used where io.Reader expected – Uvelichitel 31 mins ago @Projjol os.File is a concrete type, your file is a variable of that type, os.Stdin is also a variable of that same type. io.Reader is ...