New answers tagged

1

Your first variant is unnecessary detailed and hard to read: an IPv4 address consists of exactly 4 digit groups 4 is a small number, maybe it's worth to inline the for loop the for loop treats the case k == 0 specially the += operator for strings allocates a new string each time the code is much longer than the description in the task "join the four numbers ...


0

an exercise from the Go Tour website I found two ways to achieve the goal but I am wondering if there is no other clean way of doing it. Example 1 with range. Example 2 with a simple fmt.Sprintf Another way (Example 3): package main import ( "fmt" "strconv" ) type IPAddr [4]byte func (ip IPAddr) String() string { s := make([]...


1

In your code you often use the expressions len(m) and len(m[0]). The code would become clearer if you defined proper names for these expressions, like: func (m Matrix) Rows() int { return len(m) } func (m Matrix) Cols() int { return len(m[0]) } This way, you can write zeros := matrix.Zeros(m.Rows(), m.Cols()), which make the code operate in mathematical ...


1

When you ask if your first solution is faster, if you are talking about theory it isn't faster. To understand why you need to understand the the difference between big O complexity and actual run time. For the purposes of this question, it is important only that you realize that big O complexity does not guarantee that a piece of code with O(n^2) run time ...


-1

In Go, string character values are Unicode characters encoded in UTF-8. UTF-8 is a variable-length encoding which uses one to four bytes per character. Rewriting your ASCII code for Unicode, unique.go: package main import ( "fmt" "unicode/utf8" ) func ascii(s string) (int, bool) { if len(s) > utf8.RuneSelf { return 0, false } ...


3

This is about the most efficient strategy you can get when it comes to uniqueness checking on a string, as far as I know. It can get pretty crazy with memory inefficiency with bigger data types, but it's still O(N) time. But unless you are specifically dealing with ASCII-only strings, be careful with hardcoding the size of your filter array. Go will try to ...


5

Elegance is the wrong criterion. The key criterion is correctness, which implies readability and maintainability. As you read text, you notice misspelled words. Does that mean that you laboriously spell-check each word, letter-by-letter? In fact, most likely, you quickly and unconsciously scan the shape of the words. Only if the shape of a word seems odd do ...


3

Elegant is hard to define, especially given the small snippet of code you've provided. I'll go down the list of different ways to write it. Note: Not all of them are what I'd call "elegant", though: // as short as possible (NOT ELEGANT) func fileExists(path string) (ok bool) { if _, err := os.Stat(path); err == nil { ok = true } return } ...


10

Instead of checking with an if statement if errStat is null and then returning false: if errStat != nil { return false } return true you can return a Boolean expression: return errStat == nil


0

I think your code works. Another approach would be trying to connect to the database using the MongoDB Go Driver. import ( "context" "log" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref") func GetClient() *mongo.Client { clientOptions := options.Client().ApplyURI("mongodb://...


Top 50 recent answers are included