1"""
2 pygments.lexers.go
3 ~~~~~~~~~~~~~~~~~~
4
5 Lexers for the Google Go language.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11from pygments.lexer import RegexLexer, bygroups, words
12from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
13 Number, Punctuation, Whitespace
14
15__all__ = ['GoLexer']
16
17
18class GoLexer(RegexLexer):
19 """
20 For Go source.
21 """
22 name = 'Go'
23 url = 'https://go.dev/'
24 filenames = ['*.go']
25 aliases = ['go', 'golang']
26 mimetypes = ['text/x-gosrc']
27 version_added = '1.2'
28
29 tokens = {
30 'root': [
31 (r'\n', Whitespace),
32 (r'\s+', Whitespace),
33 (r'(\\)(\n)', bygroups(Text, Whitespace)), # line continuations
34 (r'//(.*?)$', Comment.Single),
35 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
36 (r'(import|package)\b', Keyword.Namespace),
37 (r'(var|func|struct|map|chan|type|interface|const)\b',
38 Keyword.Declaration),
39 (words((
40 'break', 'default', 'select', 'case', 'defer', 'go',
41 'else', 'goto', 'switch', 'fallthrough', 'if', 'range',
42 'continue', 'for', 'return'), suffix=r'\b'),
43 Keyword),
44 (r'(true|false|iota|nil)\b', Keyword.Constant),
45 # It seems the builtin types aren't actually keywords, but
46 # can be used as functions. So we need two declarations.
47 (words((
48 'uint', 'uint8', 'uint16', 'uint32', 'uint64',
49 'int', 'int8', 'int16', 'int32', 'int64',
50 'float', 'float32', 'float64',
51 'complex64', 'complex128', 'byte', 'rune',
52 'string', 'bool', 'error', 'uintptr', 'any', 'comparable',
53 'print', 'println', 'panic', 'recover', 'close', 'complex',
54 'real', 'imag', 'len', 'cap', 'append', 'copy', 'delete',
55 'new', 'make', 'min', 'max', 'clear'), suffix=r'\b(\()'),
56 bygroups(Name.Builtin, Punctuation)),
57 (words((
58 'uint', 'uint8', 'uint16', 'uint32', 'uint64',
59 'int', 'int8', 'int16', 'int32', 'int64',
60 'float', 'float32', 'float64',
61 'complex64', 'complex128', 'byte', 'rune',
62 'string', 'bool', 'error', 'uintptr', 'any', 'comparable'), suffix=r'\b'),
63 Keyword.Type),
64 # imaginary_lit
65 (r'\d+i', Number),
66 (r'\d+\.\d*([Ee][-+]\d+)?i', Number),
67 (r'\.\d+([Ee][-+]\d+)?i', Number),
68 (r'\d+[Ee][-+]\d+i', Number),
69 # float_lit
70 (r'\d+(\.\d+[eE][+\-]?\d+|'
71 r'\.\d*|[eE][+\-]?\d+)', Number.Float),
72 (r'\.\d+([eE][+\-]?\d+)?', Number.Float),
73 # int_lit
74 # -- octal_lit
75 (r'0[0-7]+', Number.Oct),
76 # -- hex_lit
77 (r'0[xX][0-9a-fA-F]+', Number.Hex),
78 # -- decimal_lit
79 (r'(0|[1-9][0-9]*)', Number.Integer),
80 # char_lit
81 (r"""'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
82 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'""",
83 String.Char),
84 # StringLiteral
85 # -- raw_string_lit
86 (r'`[^`]*`', String),
87 # -- interpreted_string_lit
88 (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
89 # Tokens
90 (r'(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\|'
91 r'|<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&]'
92 r'|~|\|)', Operator),
93 (r'[|^<>=!()\[\]{}.,;:]', Punctuation),
94 # identifier
95 (r'[^\W\d]\w*', Name.Other),
96 ]
97 }