What is your honest opinion about the GO language?
Thalidomide Vintage Ad Shirt $22.14 |
DMT Has Friends For Me Shirt $21.68 |
Thalidomide Vintage Ad Shirt $22.14 |
What is your honest opinion about the GO language?
Thalidomide Vintage Ad Shirt $22.14 |
DMT Has Friends For Me Shirt $21.68 |
Thalidomide Vintage Ad Shirt $22.14 |
Slower Rust, no generics, slowly dying
>slower Rust, every non-moronic still opts to use go over Rust for some reason
Too many threads about it.
no nonsense backend language, i learnt how to read golang in a month
>What is your honest opinion about the GO language?
worse Nim
>i learnt how to read golang in a month
are you moronic? that's very slow
I'd learn it for a job but I don't have anything I need to create with it right now.
It is weird
Like the for loop doesnt have parenthesis, you dont need to even declare variables and it doesnt demand you use semicolons.
Its like some thing they just whipped up for the sole purpose of making APIs
A language made by morons. I don't get why there are interfaces and structs if interfaces are just pointers to structs, so what is the point of interfaces then? And if the point of interfaces is to user late-binding why we can't overload a fricking interface?
wtf is with the daily golang threads?
is the google ai data mining our opinions so it can fix go?
>is the google ai data mining our opinions so it can fix go?
Lol they have access to the entire internet, if they wanted to do that
Im trying to find a way to check if a value is a certain type.
Something that is trivial in other programming languages. But there is nothing on google on how to do it.
I might drop thus turd and just do what i was going to do in javascript.
reflect.TypeOf
be careful though. I've done stuff like that in Go before and it becomes a mess very quickly.
Well yeah but then i type
if reflect.TypeOf(variable)=="string"
And it doesnt work
if reflect.TypeOf(variable).Kind() == reflect.String
Though just use type switches if you can.
thanks ... apparently my json has no strings though ugh
Uhh that's weird, do you have an example JSON and code?
sure here is the thing I last uploaded
for the go code:
https://github.com/peteblank/directory2todolist/blob/main/test.go
for the json:
https://raw.githubusercontent.com/peteblank/directory2todolist/main/output.json
I'm trying to get it to print result: with each individual key. It kind of does but then when it gets to the second nested thing it prints the whole thing. I don't know how to explain it...
This one runs on my machine but won't run on online compilers for some reason.
// main.go
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"reflect"
)
// The data struct for the decoded data
// Notice that all fields must be exportable!
type Users struct {
Users []Data `json:"data"`
}
type Data struct {
Type string `json:"type"`
Name string `json:"name"`
Contents []content
}
type content struct {
Type string `json:"type"`
Name string `json:"name"`
Contents []content
}
func data2(reflect.Value) {
}
func main() {
// Let's first read the `config.json` file
resp, err := http.Get("https://raw.githubusercontent.com/peteblank/directory2todolist/main/output.json")
if err != nil {
print(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
print(err)
}
//content, _ := ioutil.ReadFile("output.json")
var data Users
json.Unmarshal(body, &data)
i := -1
for range data.Users[0].Contents[0].Contents[1].Contents {
i++
log.Printf("results: %sn", data.Users[0].Contents[0].Contents[1].Contents[i])
}
}
It works for me, my guess is that online compilers impose some memory limit, because that's using 32MB of raw JSON + to hold the data. I'd recommend trying json.NewDecoder(resp.Body).Decode(&data); that way you only load the JSON structure, and not the entire response into memory.
Uhh I am not sure what your goals here are, but let me unpack some things.
Instead of doing i := -1 and then incrementing, you can have the index and value directly by doing:
for i, v := range data.Users[0].Contents[0].Contents[1].Contents {
log.Printf("results: %sn", data.Users[0].Contents[0].Contents[1].Contents[i])
}
Though you can also just skip the index:
for _, value := range data.Users[0].Contents[0].Contents[1].Content {
log.Printf("results: %sn", value)
}
Now, about the whole thing, I am not sure why you need TypeOf at all given that all your data consists of strings, basically. Given that's it's a directory structure, a recursive approach might make sense here. You can also combine Data and content into one structure and make it recursive.
Here's my naive approach on how I would handle this. It's properly also not best practice, but maybe it helps:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
)
// The data struct for the decoded data
// Notice that all fields must be exportable!
type Users struct {
Users []Data `json:"data"`
}
type Data struct {
Type string `json:"type"`
Name string `json:"name"`
Data []Data `json:"contents"`
}
func data2(reflect.Value) {
}
func main() {
// Let's first read the `config.json` file
content, _ := ioutil.ReadFile("output.json")
var data Users
json.Unmarshal(content, &data)
for i, v := range data.Users {
fmt.Printf("User %d:n", i)
if v.Type == "directory" {
handleDirectory("", v)
} else if v.Type == "file" {
fmt.Printf("t %sn", v.Name)
}
}
}
func handleDirectory(directoryName string, data Data) {
for _, v := range data.Data {
if v.Type == "directory" {
nestedDirectoryName := directoryName + "/" + v.Name
handleDirectory(nestedDirectoryName, v)
} else if v.Type == "file" {
fmt.Printf("t %s/%sn", directoryName, v.Name)
}
}
}
Thank you!
Though now that I see that there basically is only one user and also a report type, maybe something like this might be better:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
)
// The data struct for the decoded data
// Notice that all fields must be exportable!
type Dump struct {
Data []Data `json:"data"`
}
type Data struct {
Type string `json:"type"`
Name string `json:"name"`
Directories int `json:"directories"`
Files int `json:"files"`
Data []Data `json:"contents"`
}
func data2(reflect.Value) {
}
func main() {
// Let's first read the `config.json` file
content, _ := ioutil.ReadFile("output.json")
var data Dump
json.Unmarshal(content, &data)
for _, v := range data.Data {
if v.Type == "directory" {
handleDirectory("", v)
} else if v.Type == "file" {
fmt.Printf("t %sn", v.Name)
} else if v.Type == "report" {
fmt.Printf("Processed %d files and %d directoriesn", v.Files, v.Directories)
}
}
}
func handleDirectory(directoryName string, data Data) {
for _, v := range data.Data {
if v.Type == "directory" {
nestedDirectoryName := directoryName + "/" + v.Name
handleDirectory(nestedDirectoryName, v)
} else if v.Type == "file" {
fmt.Printf("t %s/%sn", directoryName, v.Name)
}
}
}
Adjusting them and maybe remove the trailing slash to have relative paths is left as an exercise 🙂
TypeOf returns a Type, not a String
I think this guy has it right
https://stackoverflow.com/questions/6996704/how-to-check-variable-type-at-runtime-in-go-language
Do you even google?
>Im trying to find a way to check if a value is a certain type
Show code example
https://go.dev/doc/effective_go#interface_conversions
https://go.dev/doc/effective_go#type_switch
That single page is enough to prepare anyone to start with Go. I wish everyone would just read it once when starting to write the code, because it pretty much covers everything about the language, and answers most of the topics/questions posted here. My first experience with Go was frustration, before I got into reasoning why everything is the way it is, and I realized that it's way more intuitive than I initially thought.
I have mixed feelings. It's faster than python and requires less resources so it can be run on cheap VPNs.
I like its concurrency, and how easy it is to write codes that can work with HTTP requests. When compared to something like C, Go outshines in this particular aspect.
I really hate their syntax. I don't want to be told how to code and that I need to use PascalCase for functions and stuff..
I really hate how anal it is about some variable and methods being declared/defined but not being used. It's not a good user experience.
I hate the verbose error checking that we need to do everywhere, but it's somewhat acceptable.
Good in its domain, that is webservices and servers. But thats mostly due to its standard library supporting that.
Error handling is fairly shit, unless you write some boilerplate global error handler for your project. Also something requiring platform specific API's is a pain in the ass. Thats when you need to use cgo, better yet just compile a C/C++ library and just link in that with a binding. So 3d rendering with OpenGl or direct3D is fairly compilcated, but possible, GUI libraries are also fairly shit, its better to just run a server doing the actual lifting while the GUI is built on html/javascript running through a browser on a local server from the standard library. The concurrency is nice and well implemented as long as you don't overdo the channel bullshit.
tl;dr great for servers and backend, way more performant than node and python in that regard, shit for general purpose stuff like games and GUI applications, the fact that it produces a single binary that does not need a runtime to run is amazing when remembering python dependency hell