al3rez / gigsib Goto Github PK
View Code? Open in Web Editor NEW📑Golang Is Good, Structure Is Bad
📑Golang Is Good, Structure Is Bad
Small detail, but your comment on casing should probably be PascalCase
since camelCase
often refers to leading with a lowercase character (with capitals acting as the camel's hump).
Liked your write up BTW - curious to see if the Go community is open to exploring a new convention like this. I'm also curious what they would recommend as a workaround for the baz
variable name assignment. Maybe the expectation (as an alternative to the single, lowercase letter) is to name it something contextual to its use case?
Take this with a grain of salt, and keep in mind this is only my personal opinion.
import "foo/bar/baz"
var baz baz.Baz
Why would you use a global variable? You're most likely going to use a local variable inside a function, and not a global var, in most cases. And when you use a local variable, the wisdom in the go community is to keep the names short. And, if we want to use the constructor, then only one repetition would be needed:
import "foo/bar/baz"
func myFunc() {
b := baz.New()
}
package baz
type Baz struct{}
func NewBaz() Baz {
// do something
}
Yes, this is bad - especially because it's best to use just func New() Baz
! When I have a function simply called New()
, I generally assume that what is being created is indicated by the package name - namely, a Baz
.
import Baz "foo/bar/baz"
var baz Baz.Type
Using this example, you are still writing baz
four times (with two different capitalisations) - no improvement from the first example, which would be the """""idiomatic""""" way to do it. The quotes are there because, here again, you're using a global variable - and most variables I've declared were certainly not globals.
Finally, the last line you wrote in the document:
Do you see a big difference between ruby and golang? (expect ! and naming convention and entrypoint)
I don't - and that's a problem! Go is not Ruby, and Ruby is not Go. I am guessing you are a beginner to Go - and trying to bring over concepts and structures from Ruby, and that is understandable. And I'm not going to stop you if you want to use this in your Go packages - I'm just some guy on the internet with opinions. But I still think it's better to think about problems for Go in Go, not in Ruby :)
Finally, here's how I would do it.
shop/models/basket.go
package models
type Basket struct {
Items []BasketItem
}
func (b *Basket) Total() (t int) {
for _, i := range b.Items {
t += i.Price
}
return
}
func (b *Basket) AddItem(i BasketItem) {
b.Items = append(b.Items, i)
}
type BasketItem struct {
Name string
Price int
}
shop/main.go
package main
import (
"fmt"
"shop/models"
)
func main() {
b := &models.Basket{}
b.AddItem(models.BasketItem{Name: "Nikelab Shoe", Price: 1337})
b.AddItem(models.BasketItem{Name: "Triple Black Shoe", Price: 420})
fmt.Println(b.Total())
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.