or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

c.mdgolang.mdindex.mdphp.mdpython.mdruby.md
tile.json

golang.mddocs/

0

# Go Functions

1

2

Go standard library functions ported to JavaScript. This module contains 4 string manipulation functions from Go's strings package.

3

4

## Capabilities

5

6

### String Functions (4 functions)

7

8

String manipulation functions from Go's strings standard library package.

9

10

```javascript { .api }

11

/**

12

* Go strings package functions

13

*/

14

golang.strings.Contains(s, substr) // Check if string contains substring

15

golang.strings.Count(s, substr) // Count non-overlapping instances

16

golang.strings.Index(s, substr) // Find first occurrence index

17

golang.strings.LastIndex(s, substr) // Find last occurrence index

18

```

19

20

**Usage Examples:**

21

22

```javascript

23

const locutus = require('locutus');

24

25

// Check substring existence

26

const hasWorld = locutus.golang.strings.Contains('Hello World', 'World'); // true

27

const hasTest = locutus.golang.strings.Contains('Hello World', 'test'); // false

28

29

// Count occurrences

30

const count = locutus.golang.strings.Count('banana', 'an'); // 2

31

const countNone = locutus.golang.strings.Count('hello', 'xyz'); // 0

32

33

// Find first occurrence

34

const firstIndex = locutus.golang.strings.Index('Hello World', 'o'); // 4

35

const notFound = locutus.golang.strings.Index('Hello World', 'xyz'); // -1

36

37

// Find last occurrence

38

const lastIndex = locutus.golang.strings.LastIndex('Hello World', 'o'); // 7

39

const lastNotFound = locutus.golang.strings.LastIndex('Hello World', 'xyz'); // -1

40

```

41

42

## Function Details

43

44

### Contains(s, substr)

45

46

Reports whether substr is within s.

47

48

```javascript { .api }

49

/**

50

* Reports whether substr is within s

51

* @param {string} s - The string to search in

52

* @param {string} substr - The substring to search for

53

* @returns {boolean} true if substr is within s, false otherwise

54

*/

55

golang.strings.Contains(s, substr)

56

```

57

58

**Parameters:**

59

- `s` (string): The string to search in

60

- `substr` (string): The substring to search for

61

62

**Returns:**

63

- (boolean): `true` if `substr` is within `s`, `false` otherwise

64

65

**Examples:**

66

```javascript

67

const locutus = require('locutus');

68

69

locutus.golang.strings.Contains('seafood', 'foo'); // true

70

locutus.golang.strings.Contains('seafood', 'bar'); // false

71

locutus.golang.strings.Contains('seafood', ''); // true

72

locutus.golang.strings.Contains('', ''); // true

73

```

74

75

### Count(s, substr)

76

77

Counts the number of non-overlapping instances of substr in s.

78

79

```javascript { .api }

80

/**

81

* Count non-overlapping instances of substr in s

82

* @param {string} s - The string to search in

83

* @param {string} substr - The substring to count

84

* @returns {number} Number of non-overlapping instances

85

*/

86

golang.strings.Count(s, substr)

87

```

88

89

**Parameters:**

90

- `s` (string): The string to search in

91

- `substr` (string): The substring to count

92

93

**Returns:**

94

- (number): The number of non-overlapping instances of `substr` in `s`

95

96

**Examples:**

97

```javascript

98

const locutus = require('locutus');

99

100

locutus.golang.strings.Count('cheese', 'e'); // 3

101

locutus.golang.strings.Count('five', ''); // 5 (number of rune boundaries)

102

locutus.golang.strings.Count('banana', 'an'); // 2

103

locutus.golang.strings.Count('banana', 'ana'); // 1 (non-overlapping)

104

```

105

106

### Index(s, substr)

107

108

Returns the index of the first instance of substr in s, or -1 if substr is not present in s.

109

110

```javascript { .api }

111

/**

112

* Return index of first instance of substr in s

113

* @param {string} s - The string to search in

114

* @param {string} substr - The substring to find

115

* @returns {number} Index of first occurrence, or -1 if not found

116

*/

117

golang.strings.Index(s, substr)

118

```

119

120

**Parameters:**

121

- `s` (string): The string to search in

122

- `substr` (string): The substring to find

123

124

**Returns:**

125

- (number): The index of the first instance of `substr` in `s`, or -1 if not found

126

127

**Examples:**

128

```javascript

129

const locutus = require('locutus');

130

131

locutus.golang.strings.Index('chicken', 'ken'); // 4

132

locutus.golang.strings.Index('chicken', 'dmr'); // -1

133

locutus.golang.strings.Index('chicken', ''); // 0

134

locutus.golang.strings.Index('', ''); // 0

135

```

136

137

### LastIndex(s, substr)

138

139

Returns the index of the last instance of substr in s, or -1 if substr is not present in s.

140

141

```javascript { .api }

142

/**

143

* Return index of last instance of substr in s

144

* @param {string} s - The string to search in

145

* @param {string} substr - The substring to find

146

* @returns {number} Index of last occurrence, or -1 if not found

147

*/

148

golang.strings.LastIndex(s, substr)

149

```

150

151

**Parameters:**

152

- `s` (string): The string to search in

153

- `substr` (string): The substring to find

154

155

**Returns:**

156

- (number): The index of the last instance of `substr` in `s`, or -1 if not found

157

158

**Examples:**

159

```javascript

160

const locutus = require('locutus');

161

162

locutus.golang.strings.LastIndex('go gopher', 'go'); // 3

163

locutus.golang.strings.LastIndex('go gopher', 'x'); // -1

164

locutus.golang.strings.LastIndex('go gopher', ''); // 9 (length of string)

165

locutus.golang.strings.LastIndex('', ''); // 0

166

```

167

168

## Use Cases

169

170

Go string functions are particularly useful for:

171

172

- **Text Processing**: Searching and analyzing text content

173

- **Data Validation**: Checking if strings contain required substrings

174

- **Log Analysis**: Finding patterns and counting occurrences in log files

175

- **URL Parsing**: Searching for specific components in URLs

176

- **Template Processing**: Finding and replacing template markers

177

178

```javascript

179

const locutus = require('locutus');

180

181

// Validate email format (basic check)

182

function hasAtSymbol(email) {

183

return locutus.golang.strings.Contains(email, '@');

184

}

185

186

// Count word occurrences in text

187

function countWord(text, word) {

188

return locutus.golang.strings.Count(text.toLowerCase(), word.toLowerCase());

189

}

190

191

// Extract substring between markers

192

function extractBetween(text, startMarker, endMarker) {

193

const startIndex = locutus.golang.strings.Index(text, startMarker);

194

if (startIndex === -1) return null;

195

196

const searchStart = startIndex + startMarker.length;

197

const endIndex = locutus.golang.strings.Index(text.slice(searchStart), endMarker);

198

if (endIndex === -1) return null;

199

200

return text.slice(searchStart, searchStart + endIndex);

201

}

202

```

203

204

## Import Patterns

205

206

```javascript

207

// Full module access

208

const locutus = require('locutus');

209

locutus.golang.strings.Contains('hello', 'lo');

210

211

// Individual function import

212

const Contains = require('locutus/golang/strings/Contains');

213

const Count = require('locutus/golang/strings/Count');

214

const Index = require('locutus/golang/strings/Index');

215

const LastIndex = require('locutus/golang/strings/LastIndex');

216

217

Contains('hello', 'lo'); // true

218

Count('banana', 'a'); // 3

219

Index('hello', 'l'); // 2

220

LastIndex('hello', 'l'); // 3

221

```