Golang

2159 readers
3 users here now

This is a community dedicated to the go programming language.

Useful Links:

Rules:

founded 1 year ago
MODERATORS
26
27
28
-8
#TT (mastodon.social)
submitted 6 months ago by [email protected] to c/[email protected]
29
 
 

Sometime around the middle of January, I stumbled across One Billion Rows Challenge. I had a lot of fun working on this. I started with an execution time of > 6min and finished at about 14s. Here’s how I went about it.

30
31
32
 
 

I've seen that a new "range-over-func" experiment is available with Go 1.22. In this article, I took a closer look and evaluated the feature for myself.

33
34
 
 

cross-posted from: https://lemmy.world/post/11504334

ServerClip is a tool for copying contents of a file over various ssh connections. Need to get a log file over to your laptop? This can help.

35
36
 
 

Hello Lemmy,

invidtui is a TUI-based Invidious client, which can:

  • Search for and browse videos, playlists and channels
  • Play audio or video from any instance
  • View, open, edit and save m3u8 playlists
  • Download video/audio in any format
  • Authenticate with the preferred instance, and show user feed, playlists and subscriptions

This release contains the following new features/fixes:


Dynamic theming

Themes can now be applied from theme files dynamically within the application as well as from command-line and configuration options.

A demo and instructions are posted here


Channel 'Releases' tab

A new 'releases' tab is added to the channel page, to show new content from channel authors.


Enhanced configuration handling

Configuration handling is now done in the most cross-platform way as possible.


I hope you enjoy this release, and any feedback is appreciated.

37
38
39
 
 

I am doing a lot of work right now that requires day and year durations and I have to define those myself. I wonder why they didn't include those in the time package.

40
41
42
43
44
 
 

Repo: https://git.code.netlandish.com/~petersanchez/gohome

I wrote a simple application to easily manage your Go modules that you host on your own domains, backed by sqlite3.

I know there are quite a few ways to do this (nginx hacks, static pages, other server apps) but none of them scratched my itch. I wanted a simple utility that I can add/edit via my web browser and be done with it. No config file changes, no server reloads, etc.

Happy to hear all feedback.

Blog post (if you care): https://petersanchez.com/easily-host-go-modules-on-your-domain/

45
 
 

Based on the Go 1.22 release notes from the Go team (3-Clause BSD License), with many interactive examples added. This blog post is synchronized with the source document as it gets updated.

46
47
 
 

Hi 👋

I have tried to learn some go but I am still very much at the beginning, like understanding how to work with variables and functions. My background is mostly in python but I am not a programmer by trade.

package main

import (
	"crypto/sha256"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
)

func write_lines_to_file(lines []string, output_file string) {
	f, err2 := os.Create(output_file)
	if err2 != nil {
		log.Fatal(err2)
	}
	defer f.Close()
	for _, line := range lines {
		_, err := f.WriteString(line + "\n")
		if err != nil {
			log.Fatal(err2)
		}
	}
}

func get_size_and_hash(file_path string) (int, string) {
	file, err := os.Open(file_path)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	hash := sha256.New()
	if _, err := io.Copy(hash, file); err != nil {
		panic(err)
	}
	sum := fmt.Sprintf("%x", hash.Sum(nil))
	file, err2 := os.Open(file_path)
	if err2 != nil {
		log.Fatal(err2)
	}
	fi, err2 := file.Stat()
	if err != nil {
		log.Fatal(err2)
	}
	my_size := fi.Size()
	return int(my_size), string(sum)
}

func get_list_of_files(target_directory string) []string {
	var files []string
	err := filepath.Walk(target_directory, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			fmt.Println(err)
			return nil
		}
		if !info.IsDir() {
			files = append(files, path)
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
	return files
}

func main() {
	// accept directory as user input
	target_directory := os.Args[1]

	my_files := get_list_of_files(target_directory)

	var content []string

	for _, file := range my_files {
		size, hash := get_size_and_hash(file)
		var str_file string = string(file)
		str_size := fmt.Sprint(size)
		var str_hash string = string(hash)
		// structure: file, checksum, size
		combined_line := str_file + "," + str_hash + "," + str_size
		content = append(content, combined_line)
	}

	var output_file string = target_directory + "/PULP_MANIFEST"
	write_lines_to_file(content, output_file)
}

I am testing this using the following command: rm -f test_input/PULP_MANIFEST && go fmt pulp_manifest.go && go build pulp_manifest.go && ./pulp_manifest test_input && cat test_input/PULP_MANIFEST on Fedora with go 1.20

Known Limitations

  • My rewrite does not handle files or directories with "," yet.

Untested

  • Files with binary content
  • Paths on macOS or Microsoft Windows
  • Paths with whitespace
  • Symlinks in target_directory
  • target_directory as symlink

I am looking for the following feedback:

  • bugs and limitations
  • a was to add tests: do you have any recommendations for talks or blog posts?
  • style & best practice
  • a way to use static typing?!
  • anything else that you would recommend a novice.

Right now, I believe my rewrite works. Feel free to shatter my assumption. Cheers.

48
 
 

Not OC

49
50
view more: ‹ prev next ›