[ prog / sol / mona ]

prog


Go(lang) Thread

1 2019-12-25 23:35

Is any of you using it? If so, what are you building?

At the moment I'm trying to replicate this website. It's the first time I'm using Go and I'm finding it pretty amusing.

2 2019-12-26 02:20 *

>>1
I started to learn it once. It's an interesting option. I don't know what I'd use for concurrency: Erlang, Go, Lua or (((Scheme)))

3 2019-12-26 19:16

>>2
I think I'd choose Go to do it. It feels very natural.

4 2019-12-26 22:41

I've been messing around with Go for a month or so now and I'm taking quite a liking to it, at least compared to what I used before it.

5 2019-12-26 22:57

>>4

What have you been doing with it?

6 2019-12-28 03:27

>>5 General web development projects. I feel like it's a pretty good tool for the job for my case.

7 2019-12-28 21:39 *

Cff

8 2019-12-30 20:43

>>7 What's this?

9 2019-12-31 09:08

>>6
Are you using fasthttp or net/http? What made you choose one over the other?

10 2019-12-31 16:43

aea

11 2020-01-11 22:48

It's pretty great, especially for web development. Web development is a bit impractical to do in C, but golang basically enables you do to a C-style for it, given it's extremely apt standard library for the task. For anything else though I'd probably just go with C.

12 2020-01-13 08:19

Unless you have millions of connections each second, there's almost no reasons to not use the better known stdlib net/http.

13 2020-01-14 04:23

>>9 I've always used net/http, I don't see much of a reason to use otherwise like >>12 mentioned.

14 2020-01-15 00:24

I like the syntax and the standard library but I hate messing with slices. You need to do the following to add an item to a slice:

a = append(a, item)

But when it comes to removing an item there's no

remove(a, index)

to remove an item.

15 2020-01-21 03:49 *

s

16 2020-01-22 22:51

Yeah, that's wierd but the trick is to re-use append when removing like this:

a = append(a[:index], a[:index+1]...)

as described here: https://github.com/golang/go/wiki/SliceTricks.

17 2020-01-22 22:51

>>16 was meant for >>14

18 2020-01-24 21:26

>>16 I have this wiki in a cheatsheet but it's rather uncomfortable to write this syntax. Nevertheless I find Go pretty cool to write toy projects on.

19 2020-02-28 22:09

I made a program that I use with i3lock.

package main

import (
	"flag"
	"fmt"
	"image"
	"image/png"
	"log"
	"os"

	"golang.org/x/image/draw"
)

var (
	flagImage  = flag.String("i", "", "Path to the image you want to pixelate")
	flagOutput = flag.String("o", "", "Path to the output image")
	flagScale  = flag.Int("p", 0, "How much you want to pixelate. [1-100]")
)

func init() {
	flag.Parse()

	if *flagImage == "" || *flagOutput == "" {
		flag.Usage()
		os.Exit(1)
	}

	if *flagScale < 1 || *flagScale > 100 {
		fmt.Println("the -p flag must be between 1-100")
		os.Exit(1)
	}
}

func main() {
	img, err := OpenImage(*flagImage)
	if err != nil {
		log.Fatalf("could not open image '%s': %s\n", *flagImage, err)
	}

	scaledDown := Rect(img, 100-*flagScale)
	out := ScaleTo(img, scaledDown)

	originalSize := Rect(img, 100)
	out = ScaleTo(out, originalSize)

	err = SaveImage(*flagOutput, out)
	if err != nil {
		log.Fatalf("could not save file to '%s': %s\n", *flagOutput, err)
	}
}

func Rect(img image.Image, scale int) image.Rectangle {
	s := float64(scale) / 100
	x := int(s * float64(img.Bounds().Max.X))
	y := int(s * float64(img.Bounds().Max.Y))
	return image.Rect(0, 0, x, y)
}

func ScaleTo(from image.Image, rect image.Rectangle) image.Image {
	to := image.NewRGBA(rect)
	draw.NearestNeighbor.Scale(to, rect, from, from.Bounds(), draw.Over, nil)
	return to
}

func SaveImage(path string, img image.Image) error {
	file, err := os.Create(path)
	if err != nil {
		return err
	}
	defer file.Close()

	err = png.Encode(file, img)
	if err != nil {
		return err
	}

	return nil
}

func OpenImage(path string) (image.Image, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	img, err := png.Decode(file)
	if err != nil {
		return nil, err
	}

	return img, nil
}

I use it with this script:

#!/usr/bin/bash

VALUE=$1
IMG=/tmp/i3lock.png
maim /tmp/i3lock.png
pixelate -i $IMG -o $IMG -p $VALUE
i3lock -i $IMG
rm $IMG
20 2020-02-28 22:32

>>19
Came back here to bump this thread and ask how Go was going for you. Talk about synchronicities.

21 2020-02-29 01:30

synchronicities

I didn't know about it. Thanks for introducing it to me!

It's been really good. I'm a miserable programmer so I've just understand generics and I'm already missing it. This program is an example of the lack of generics as methods for gif, jpg and png are all the same and I can't wrap them around a generic function. I don't know how to do it with interfaces because I know I have to type structs with the same method for all of them just to get an "unified" function call. Not worth it for a small program like that.
Nevertheless I really enjoy the syntax and the tooling. Having a standard formatter and a whole lot of architecture support out of the box is something I appreciate.

But tell me about you, anon. How has it been for you?

22 2020-02-29 04:55

>>21
I have yet to learn it properly and write small programs, like you did. I was curious about your progress and thought to ask you how you were doing. Then I saw that you came back here half an hour before me, answering all those questions.

Have you already grasped Go's model of concurrency?

23 2020-02-29 19:30

>>22

Have you already grasped Go's model of concurrency?

Yeah, it's not hard at all. My main problem is not knowing when to apply it. But as far as I can tell I don't plan to take Go too far. I like to use it to write small programs.

https://fasterthanli.me/blog/2020/i-want-off-mr-golangs-wild-ride/

I stumbled across most of the problems mentioned on this article.

24 2020-10-14 18:43

Any news on Go 2?

25


VIP:

do not edit these