Go

1 program Added 2025-10-22T09:33:12Z Model: openai/gpt-4o-miniTemp: 0.4 Evidence Report issue View issues
Aliases: Golang
Provenance: commit 594af007f9 · authored 2025-10-22T11:33:12+02:00 · model openai/gpt-4o-mini

Sources mentioning this language

8 sources · pl_id: pl/go
LLM (this repo) · 1PldbLinguistPygmentsWikipediaHyperpolyglotRosettacodeWikidata · Q37227

Wikipedia infobox

Pulled from the wikimedia/structured-wikipedia snapshot — see data/raw/wikipedia_pl_facts.*.jsonl and pl_fact.csv for the long-table provenance.

Paradigmsmulti-paradigm: concurrent · imperative · functional · object-oriented
Typinginferred, static, strong, structural, nominal
Designed byThe Go Authors · Robert Griesemer Rob Pike Ken Thompson
First appeared2009
Influenced byC · Oberon-2 · Limbo · Active Oberon · communicating sequential processes · Pascal · Oberon · Smalltalk · Newsqueak · Modula-2 · Alef · APL · BCPL · Modula · occam
License3-clause BSD + patent grant
Implemented inGo · Assembly language (gc) · C++ (gofrontend)
Homepagehttps://go.dev

Extensions claimed by this language

3 claims. Each row is one upstream assertion with its strength. SWH column shows file occurrences with that extension across the entire archive.
ExtensionSourceStrengthSWH
.golinguistprimary112.6M files
.gopygmentsprimary112.6M files
.gowikidataprimary112.6M files

Related languages

Golog (0.31)Slang (0.31)Pluto (0.28)D (0.27)Fetlang (0.27)

LLM-contributed programs

Hello World

Provenance: commit 594af007f9 · authored 2025-10-22T11:33:12+02:00 · model openai/gpt-4o-mini · Temp 0.4
code.go · license: MIT · added: 2025-10-22T09:33:12Z
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Real programs from Software Heritage

1 sample mined from derived_datasets/<date>/contents/*.parquet, byte-verified against the SWH archive. Citation-grade qualified SWHIDs preserved.
tree-walk.go · 10416 B · ext .go · seen 3842× in SWH
via unique-primary
swh:1:cnt:aa559990f2685f2948216ba141a2068b0cdae9db;origin=https://github.com/0chain/zs3server;anchor=swh:1:rev:069432566fcfac1f1053677cc925ddafd750730a;path=/cmd/tree-walk.go
Open in SWH · Raw bytes (SWH) · GitHub raw
Show source
// Copyright (c) 2015-2021 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
	"context"
	"sort"
	"strings"
)

// TreeWalkResult - Tree walk result carries results of tree walking.
type TreeWalkResult struct {
	entry      string
	isEmptyDir bool
	end        bool
}

// Return entries that have prefix prefixEntry.
// The supplied entries are modified and the returned string is a subslice of entries.
func filterMatchingPrefix(entries []string, prefixEntry string) []string {
	if len(entries) == 0 || prefixEntry == "" {
		return entries
	}
	// Write to the beginning of entries.
	dst := entries[:0]
	for _, s := range entries {
		if !HasPrefix(s, prefixEntry) {
			continue
		}
		dst = append(dst, s)
	}
	return dst
}

// xl.ListDir returns entries with trailing "/" for directories. At the object layer
// we need to remove this trailing "/" for objects and retain "/" for prefixes before
// sorting because the trailing "/" can affect the sorting results for certain cases.
// Ex. lets say entries = ["a-b/", "a/"] and both are objects.
//     sorting with out trailing "/" = ["a", "a-b"]
//     sorting with trailing "/"     = ["a-b/", "a/"]
// Hence if entries[] does not have a case like the above example then isLeaf() check
// can be delayed till the entry is pushed into the TreeWalkResult channel.
// delayIsLeafCheck() returns true if isLeaf can be delayed or false if
// isLeaf should be done in listDir()
func delayIsLeafCheck(entries []string) bool {
	for i, entry := range entries {
		if HasSuffix(entry, globalDirSuffixWithSlash) {
			return false
		}
		if i == len(entries)-1 {
			break
		}
		// If any byte in the "entry" string is less than '/' then the
		// next "entry" should not contain '/' at the same same byte position.
		for j := 0; j < len(entry); j++ {
			if entry[j] < '/' {
				if len(entries[i+1]) > j {
					if entries[i+1][j] == '/' {
						return false
					}
				}
			}
		}
	}
	return true
}

// ListDirFunc - "listDir" function of type listDirFunc returned by listDirFactory() - explained below.
type ListDirFunc func(bucket, prefixDir, prefixEntry string) (emptyDir bool, entries []string, delayIsLeaf bool)

// IsLeafFunc - A function isLeaf of type isLeafFunc is used to detect if an
// entry is a leaf entry. There are 2 scenarios where isLeaf should behave
// differently depending on the backend:
// 1. FS backend object listing - isLeaf is true if the entry
//    has no trailing "/"
// 2. Erasure backend object listing - isLeaf is true if the entry
//    is a directory and contains xl.meta
type IsLeafFunc func(string, string) bool

// IsLeafDirFunc - A function isLeafDir of type isLeafDirFunc is used to detect
// if an entry is empty directory.
type IsLeafDirFunc func(string, string) bool

func filterListEntries(bucket, prefixDir string, entries []string, prefixEntry string, isLeaf IsLeafFunc) ([]string, bool) {
	// Filter entries that have the prefix prefixEntry.
	entries = filterMatchingPrefix(entries, prefixEntry)

	// Listing needs to be sorted.
	sort.Slice(entries, func(i, j int) bool {
		if !HasSuffix(entries[i], globalDirSuffixWithSlash) && !HasSuffix(entries[j], globalDirSuffixWithSlash) {
			return entries[i] < entries[j]
		}
		first := entries[i]
		second := entries[j]
		if HasSuffix(first, globalDirSuffixWithSlash) {
			first = strings.TrimSuffix(first, globalDirSuffixWithSlash) + slashSeparator
		}
		if HasSuffix(second, globalDirSuffixWithSlash) {
			second = strings.TrimSuffix(second, globalDirSuffixWithSlash) + slashSeparator
		}
		return first < second
	})

	// Can isLeaf() check be delayed till when it has to be sent down the
	// TreeWalkResult channel?
	delayIsLeaf := delayIsLeafCheck(entries)
	if delayIsLeaf {
		return entries, true
	}

	// isLeaf() check has to happen here so that trailing "/" for objects can be removed.
	for i, entry := range entries {
		if isLeaf(bucket, pathJoin(prefixDir, entry)) {
			entries[i] = strings.TrimSuffix(entry, slashSeparator)
		}
	}

	// Sort again after removing trailing "/" for objects as the previous sort
	// does not hold good anymore.
	sort.Slice(entries, func(i, j int) bool {
		if !HasSuffix(entries[i], globalDirSuffix) && !HasSuffix(entries[j], globalDirSuffix) {
			return entries[i] < entries[j]
		}
		first := entries[i]
		second := entries[j]
		if HasSuffix(first, globalDirSuffix) {
			first = strings.TrimSuffix(first, globalDirSuffix) + slashSeparator
		}
		if HasSuffix(second, globalDirSuffix) {
			second = strings.TrimSuffix(second, globalDirSuffix) + slashSeparator
		}
		if first == second {
			return HasSuffix(entries[i], globalDirSuffix)
		}
		return first < second
	})
	return entries, false
}

// treeWalk walks directory tree recursively pushing TreeWalkResult into the channel as and when it encounters files.
func doTreeWalk(ctx context.Context, bucket, prefixDir, entryPrefixMatch, marker string, recursive bool, listDir ListDirFunc, isLeaf IsLeafFunc, isLeafDir IsLeafDirFunc, resultCh chan TreeWalkResult, endWalkCh <-chan struct{}, isEnd bool) (emptyDir bool, treeErr error) {
	// Example:
	// if prefixDir="one/two/three/" and marker="four/five.txt" treeWalk is recursively
	// called with prefixDir="one/two/three/four/" and marker="five.txt"

	var markerBase, markerDir string
	if marker != "" {
		// Ex: if marker="four/five.txt", markerDir="four/" markerBase="five.txt"
		markerSplit := strings.SplitN(marker, SlashSeparator, 2)
		markerDir = markerSplit[0]
		if len(markerSplit) == 2 {
			markerDir += SlashSeparator
			markerBase = markerSplit[1]
		}
	}

	emptyDir, entries, delayIsLeaf := listDir(bucket, prefixDir, entryPrefixMatch)
	// When isleaf check is delayed, make sure that it is set correctly here.
	if delayIsLeaf && isLeaf == nil || isLeafDir == nil {
		return false, errInvalidArgument
	}

	// For an empty list return right here.
	if emptyDir {
		return true, nil
	}

	// example:
	// If markerDir="four/" Search() returns the index of "four/" in the sorted
	// entries list so we skip all the entries till "four/"
	idx := sort.Search(len(entries), func(i int) bool {
		return entries[i] >= markerDir
	})
	entries = entries[idx:]
	// For an empty list after search through the entries, return right here.
	if len(entries) == 0 {
		return false, nil
	}

	for i, entry := range entries {
		var leaf, leafDir bool

		// Decision to do isLeaf check was pushed from listDir() to here.
		if delayIsLeaf {
			leaf = isLeaf(bucket, pathJoin(prefixDir, entry))
			if leaf {
				entry = strings.TrimSuffix(entry, slashSeparator)
			}
		} else {
			leaf = !HasSuffix(entry, slashSeparator)
		}

		if HasSuffix(entry, slashSeparator) {
			leafDir = isLeafDir(bucket, pathJoin(prefixDir, entry))
		}

		isDir := !leafDir && !leaf

		if i == 0 && markerDir == entry {
			if !recursive {
				// Skip as the marker would already be listed in the previous listing.
				continue
			}
			if recursive && !isDir {
				// We should not skip for recursive listing and if markerDir is a directory
				// for ex. if marker is "four/five.txt" markerDir will be "four/" which
				// should not be skipped, instead it will need to be treeWalk()'ed into.

				// Skip if it is a file though as it would be listed in previous listing.
				continue
			}
		}
		if recursive && isDir {
			// If the entry is a directory, we will need recurse into it.
			markerArg := ""
			if entry == markerDir {
				// We need to pass "five.txt" as marker only if we are
				// recursing into "four/"
				markerArg = markerBase
			}
			prefixMatch := "" // Valid only for first level treeWalk and empty for subdirectories.
			// markIsEnd is passed to this entry's treeWalk() so that treeWalker.end can be marked
			// true at the end of the treeWalk stream.
			markIsEnd := i == len(entries)-1 && isEnd
			emptyDir, err := doTreeWalk(ctx, bucket, pathJoin(prefixDir, entry), prefixMatch, markerArg, recursive,
				listDir, isLeaf, isLeafDir, resultCh, endWalkCh, markIsEnd)
			if err != nil {
				return false, err
			}

			// A nil totalFound means this is an empty directory that
			// needs to be sent to the result channel, otherwise continue
			// to the next entry.
			if !emptyDir {
				continue
			}
		}

		// EOF is set if we are at last entry and the caller indicated we at the end.
		isEOF := ((i == len(entries)-1) && isEnd)
		select {
		case <-endWalkCh:
			return false, errWalkAbort
		case resultCh <- TreeWalkResult{entry: pathJoin(prefixDir, entry), isEmptyDir: leafDir, end: isEOF}:
		}
	}

	// Everything is listed.
	return false, nil
}

// Initiate a new treeWalk in a goroutine.
func startTreeWalk(ctx context.Context, bucket, prefix, marker string, recursive bool, listDir ListDirFunc, isLeaf IsLeafFunc, isLeafDir IsLeafDirFunc, endWalkCh <-chan struct{}) chan TreeWalkResult {
	// Example 1
	// If prefix is "one/two/three/" and marker is "one/two/three/four/five.txt"
	// treeWalk is called with prefixDir="one/two/three/" and marker="four/five.txt"
	// and entryPrefixMatch=""

	// Example 2
	// if prefix is "one/two/th" and marker is "one/two/three/four/five.txt"
	// treeWalk is called with prefixDir="one/two/" and marker="three/four/five.txt"
	// and entryPrefixMatch="th"

	resultCh := make(chan TreeWalkResult, maxObjectList)
	entryPrefixMatch := prefix
	prefixDir := ""
	lastIndex := strings.LastIndex(prefix, SlashSeparator)
	if lastIndex != -1 {
		entryPrefixMatch = prefix[lastIndex+1:]
		prefixDir = prefix[:lastIndex+1]
	}
	marker = strings.TrimPrefix(marker, prefixDir)
	go func() {
		isEnd := true // Indication to start walking the tree with end as true.
		doTreeWalk(ctx, bucket, prefixDir, entryPrefixMatch, marker, recursive, listDir, isLeaf, isLeafDir, resultCh, endWalkCh, isEnd)
		close(resultCh)
	}()
	return resultCh
}

Contribute — propose a file extension

Tell us where to find evidence about Go (mapped to pl/go). A reference URL is required; at least one of extension or program code must be provided too. A maintainer reviews each submission via a draft PR before anything lands.
Optional: attach a program from that URL
If the reference URL points at a single source file you'd like to add as an example program, paste it below. The workflow will write it under languages/Go/programs/<sha>/. Keep under ~200 lines.
(or open the pre-filled issue directly)
← Gnuplot Go (programming language) →